How to configure the log4j output file path in web.xml and log4j.properties?

怎甘沉沦 提交于 2019-12-22 04:49:12

问题


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

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>

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;

}

}

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

But my logs are not created in C: drive.

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


回答1:


You need to include-

    <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>

in your web.xml. then it will work fine.




回答2:


Your question have nothing to do with Spring MVC. It is simply a Log4J configuration issue. (Please edit your question)

Have you setup the logger correctly? (e.g. to allow Debug level, and set your file appender to the logger)

Have something like this in your config (I just recall the syntax by memory, fix it yourself if there is anything wrong)

log4j.rootLogger.level=INFO, file
log4j.logger.your.package.RegController=DEBUG

something like that.

Also, make sure that you don't have other log4j.properties or log4j.xml in classpath. Log4J will probably load them instead of yours, which caused the problem.




回答3:


Include following log4j.properties file in resources folder.

log4j.rootCategory=INFO,S,rollingFile

log4j.appender.S =org.apache.log4j.ConsoleAppender
log4j.appender.S.layout =org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n

log4j.appender.rollingFile = org.apache.log4j.DailyRollingFileAppender

#provide path to your location where you want logs created. For now its logs folder of   tomcat.
log4j.appender.rollingFile.File = ${catalina.home}/logs/loging.log
log4j.appender.rollingFile.Append = true
log4j.appender.rollingFile.MaxFileSize=2000KB 
log4j.appender.rollingFile.MaxBackupIndex=9 

log4j.appender.rollingFile.Threshold = ALL

log4j.appender.rollingFile.DatePattern = '.'yyy-MM-dd
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n

Replace your code with this one. Definitely it will work fine.




回答4:


in your web.xml add this code

   <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>

and under the /WEB-INF/resource/ add the file log4j.properties with this code

 # Root logger option 
   log4j.rootLogger=DEBUG, file # Redirect log messages to a log file
   log4j.appender.file=org.apache.log4j.RollingFileAppender #outputs to
   Tomcat home log4j.appender.file.File=${catalina.home}/logs/myapp.log
   log4j.appender.file.MaxFileSize=5MB
   log4j.appender.file.MaxBackupIndex=10
   log4j.appender.file.layout=org.apache.log4j.PatternLayout
   log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}
   %-5p %c{1}:%L - %m%n

and in your Class add this code

private static final Logger logger = Logger.getLogger(NameOfYourrClass.class);

and inside the your function add this code

logger.debug("This is Error message", new Exception("Testing"));


来源:https://stackoverflow.com/questions/13153004/how-to-configure-the-log4j-output-file-path-in-web-xml-and-log4j-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!