简体   繁体   English

如何为Tomcat设置Spring日志

[英]How to setup Spring Logs for Tomcat

Working on Spring MVC and not having Spring logs has made it hard to debug. 使用Spring MVC且没有Spring日志使调试变得很困难。 I have read few other articles on this problem and none seem to help me. 我读过其他一些关于此问题的文章,似乎都没有帮助。 log4j.properties is in src folder. log4j.propertiessrc文件夹中。 slf4j-api-1.5.11 , slf4j-log4j12-1.5.11 , slf4j-simple-1.5.11 , commons-logging-1.1.jar and log4j-1.2.16.jar jars are in the classpath. slf4j-api-1.5.11 slf4j-log4j12-1.5.11slf4j-log4j12-1.5.11slf4j-simple-1.5.11commons-logging-1.1.jarlog4j-1.2.16.jar jar在类路径中。
Log4j content is: Log4j的内容是:

log4j.rootLogger=INFO, console


# Console appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
log4j.throwableRenderer=org.apache.log4j.EnhancedThrowableRenderer

But I don't see any Spring logs in my console. 但是我在控制台中看不到任何Spring日志。

Note: Using Spring 3.1 注意:使用Spring 3.1

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. 删除commons-logging-1.1.jar并添加jcl-over-slf4j-1.5.11.jar ,因为您需要所有日志记录调用以通过slf4j,然后由log4j处理。

Also, you will need to add loggers for spring in log4j.properties, as indicated below. 另外,您将需要在log4j.properties中添加spring的记录器,如下所示。 log4j.properties needs to end up in tomcat/webapps/<application>/WEB-INF/classes . log4j.properties需要以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). Maven依赖项需要包含类似于以下内容的条目(摘自“ 使用SLF4J”部分)。
Note the exclusion of commons-logging and inclusion of jcl-over-slf4j . 注意排除了commons-loggingjcl-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 . 还要将console更改为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.properties放入“ src”文件夹时,我没有任何弹簧日志和消息:

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. 当我移回src / main / resources时-一切正常。 Seems like log4j.properties must be placed at "classes" folder after deploy. 部署后,似乎必须将log4j.properties放在“ classes”文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM