如何在web.xml和log4j.properties中配置log4j输出文件路径? [英] How to configure the log4j output file path in web.xml and log4j.properties?

查看:254
本文介绍了如何在web.xml和log4j.properties中配置log4j输出文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个可以注册员工的网络应用程序。

I have developed a web application where i can register a employee.

我的 web.xml p>

My web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <display-name>Employee Registration</display-name>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
  <servlet-name>sapient</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
  <servlet-name>sapient</servlet-name>
  <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

</web-app>

现在当我点击注册按钮在我的html页面。它转到一个控制器类,我已经写了一个代码用于日志记录,

Now when I click on a Register Button in my html page. It goes to a controller class where I have wrote a code for logging,

@org.springframework.stereotype.Controller
public class RegController  {
    private static final Logger LOGGER = Logger
            .getLogger(RegController.class.getName());


@RequestMapping(value = "/register.htm", method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    LOGGER.debug("ENTERING the contoller class");
    ModelAndView mav = new ModelAndView("register");
    LOGGER.debug("exiting the contoller class");
    return mav;

}

}

我已经创建了一个名为 resource 的包,并在

I have created a package called resource and have created a log4j.properties file in

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

但我的日志不是在 C:驱动器中创建的。

But my logs are not created in C: drive.

我需要在web.xml中配置一些东西吗?
我已经包含了log4j.jar文件。

Do I need to configure something in web.xml ? I have already included log4j.jar file.

推荐答案

您需要包括 -

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/resource/log4j.properties</param-value>
    </context-param>

     <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

。那么它会正常工作。

这篇关于如何在web.xml和log4j.properties中配置log4j输出文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆