Integrating logger with Spring Template MVC projec

2019-09-07 15:50发布

I read a lot of posts and tried a lot of approaches but still I am unable to log my code flow. The reason could be that I got confused due to various opinions.

My WebContent/conf/log4j.properties

# Direct log messages to a log file
log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=D:/mylog.log
log4j.appender.rollingFile.MaxFileSize=2MB
log4j.appender.rollingFile.MaxBackupIndex=2
log4j.appender.rollingFile.Threshold=debug
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c - %m%n
log4j.rootLogger = INFO, rollingFile

My /WEB-INF/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>SpringSort</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

  <servlet>
        <servlet-name>sort</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>sort</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>

A part of my controller Controller.java

private static final Logger logger = LoggerFactory.getLogger(Controller.class);
System.out.println(logger);
System.out.println(logger.isDebugEnabled());
logger.info("Logger Initiliazed");

The System.out.println(logger); gives a reference address and the
System.out.println(logger.isDebugEnabled()); gives true. But no log file is being created.

Can anyone please help me out of this?

1条回答
来,给爷笑一个
2楼-- · 2019-09-07 16:13

Depencies mentioned in the tutorial are for Maven (so if you don't use maven just be sure to have log4j jars in your buildpath).

Another simple tutorial to add log4j to your project and then on how to configure it.

查看更多
登录 后发表回答