【问题标题】:tomcat log is strangetomcat日志很奇怪
【发布时间】:2021-06-22 10:20:58
【问题描述】:

我的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());

那么问题出在哪里?

【问题讨论】:

  • 请添加您的dailyFileHibernate 附加程序的定义。
  • @Piotr P. Karwasz 它已经添加了

标签: tomcat log4j


【解决方案1】:

我发现了问题,tomcat conf文件夹中的context.xml已经被一些人修改了。

<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 ?

【讨论】:

  • 在很早的阶段 (Catalina#initStreams) Tomcat 用自己的包装器替换了 System.out 对象。之后初始化的所有内容,例如您的org.apache.log4j.ConsoleAppender,将根据swallowOutput 的值使用真正的标准输出(catalina.out)或JULI。如果要一路使用Log4j到文件,需要安装log4j-appserver
猜你喜欢
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 2015-05-21
  • 2017-03-16
  • 1970-01-01
  • 2013-06-11
  • 2019-01-03
相关资源
最近更新 更多