tomcat日志很奇怪

kiz8lqtg  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(350)

我的spring应用程序使用log4j,它在tomcat 8.5.31中运行,默认配置。
这个 logger.info(foo) 在com.foo.foo1.a.java中,它是在localhost.log中打印的,而不是在catalina.out中打印的。
但是 logger.info(foo) 在com.foo.foo2.b.java中,是在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());

那么问题出在哪里呢?

ecfdbz9o

ecfdbz9o1#

我发现了这个问题,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?

相关问题