tomcat日志很奇怪



我的spring应用程序使用log4j,它在tomcat 8.5.31中运行,配置是默认的。

com.foo.foo1.A.java中的logger.info(foo)在localhost.log中打印,在catalina.out.中不打印

但是com.foo.foo2.B.java中的logger.info(foo)在catalina.out 中打印

log4j.properties:

# This is the configuring for logging displayed in the Application Server
log4j.rootLogger=INFO, stdout, dailyFile,Hibernate
#stdout configure
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern= %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
log4j.appender.dailyFile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyFile.File = /xx/xx.log
log4j.appender.dailyFile.Append = true
log4j.appender.dailyFile.Threshold = DEBUG
log4j.appender.dailyFile.layout = org.apache.log4j.PatternLayout
log4j.appender.dailyFile.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss-SSS} [%t] [%c]-[%p] %m%n
log4j.logger.org.springframework=ERROR
# Changing the log level to DEBUG will display SQL Hibernate generated
log4j.logger.org.hibernate=ERROR
log4j.logger.org.hibernate.SQL=ERROR,Hibernate
log4j.logger.org.hibernate.tool=ERROR
log4j.logger.org.hibernate.cache=ERROR
log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
log4j.logger.org.hibernate.type.descriptor.sql.BasicExtractor=DEBUG
log4j.logger.org.hibernate.engine.QueryParameters=TRACE
log4j.logger.org.hibernate.engine.query.HQLQueryPlan=ERROR
log4j.appender.Hibernate=org.apache.log4j.RollingFileAppender
log4j.appender.Hibernate.File=/xx/xxx.log
log4j.appender.Hibernate.layout=org.apache.log4j.PatternLayout
log4j.appender.Hibernate.layout.ConversionPattern= %-d{yyyy-MM-dd HH:mm:ss-SSS} [%t] [%c]-[%p] %m%n
log4j.appender.Hibernate.Append=true
log4j.appender.Hibernate.MaxFileSize=2MB
log4j.appender.Hibernate.MaxBackupIndex=5

A.java、B.java

private final org.slf4j.Logger logger  = org.slf4j.LoggerFactory.getLogger(this.getClass());

那么问题出在哪里呢?

我发现了问题,一些人修改了context.xml,它位于tomcat conf文件夹中。

<Context swallowOutput="true">

删除swallowOutput="true"后,日志现在在catalina.out 中正常打印

tomcat官方网站描述为:

Old applications that still use System.out or System.err can be tricked by setting swallowOutput attribute on a Context. If the attribute is set to true, the calls to System.out/err during request processing will be intercepted, and their output will be fed to the logging subsystem using the javax.servlet.ServletContext.log(...) calls.
Note, that the swallowOutput feature is actually a trick, and it has its limitations. It works only with direct calls to System.out/err, and only during request processing cycle. It may not work in other threads that might be created by the application. It cannot be used to intercept logging frameworks that themselves write to the system streams, as those start early and may obtain a direct reference to the streams before the redirection takes place.

它只适用于直接调用System.out/err,似乎适用于log4j?

最新更新