How to setup Spring Logs for Tomcat

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 10:09:57

Remove commons-logging-1.1.jar and add jcl-over-slf4j-1.5.11.jar, as you need all logging calls to go through slf4j and then handled by log4j.

Also, you will need to add loggers for spring in log4j.properties, as indicated below. log4j.properties needs to end up in tomcat/webapps/<application>/WEB-INF/classes.

#Spring Framework
log4j.logger.org.springframework=INFO
log4j.logger.org.springframework.oxm=INFO
log4j.logger.org.springframework.transaction=WARN

Maven dependencies need to contain entries similar to following (taken from Using SLF4J section).
Note the exclusion of commons-logging and inclusion of jcl-over-slf4j.

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>3.1.2.RELEASE</version>
  <scope>runtime</scope>
  <exclusions>
     <exclusion>
       <groupId>commons-logging</groupId>
       <artifactId>commons-logging</artifactId>
     </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
  <version>1.7.0</version>
  <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.0</version>
  <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.7.0</version>
  <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.14</version>
  <scope>runtime</scope>
</dependency>

Add this...

log4j.appender.stdout.Target=System.out

Also change console to stdout. See example

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

When I put log4j.properties to "src" folder I have no spring logs and message:

log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

When I moved back to src/main/resources - all works fine. Seems like log4j.properties must be placed at "classes" folder after deploy.

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