log4j interference - level keeps being set to “OFF

2019-08-15 07:35发布

I'm trying to set up log4j in my app and one of my third party JARs seems to somehow keep turning my log level to "OFF". It only happens when code from that library is executed. I don't know how it can do this because I'm not using a logger it knows the name of, and I'm not using the root logger. I've spent 5 hours on this and so far my only achievement has been to acquire a frustration level of over 9000.

I'm using the latest Tomcat (7.0.37) on windows 7 64 bit, Java 6 u23.

log4j.properties:

log4j.logger.com.foo.bar.SearchAndReport=TRACE, stdout, file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:/logTest.log
log4j.appender.file.MaxFileSize=100KB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p] %d{dd MMM yyyy HH:mm:ss,SSS} - %C{2}.%M() - %m %n

then in my Servlet for testing I have:

Logger log = Logger.getLogger(SearchAndReport.class);
System.out.println(log.getLevel());

This code runs every time a DoPost or DoGet is run, and the level stays on the level I set (TRACE) until the third party code runs and turns it to off.

My questions are:

  1. How can another logger interfere with a logger I've named and set up myself?
  2. How can I stop this happening, and force the level I set?

Thanks :)

1条回答
爷的心禁止访问
2楼-- · 2019-08-15 08:11
  1. Log4j configurations are not merged, it seems that either the other configuration is present and is read instead of yours, or the configuration is overwritten. Embedding log4j config file in JAR is source of many similar problems.

  2. You can reload the configuration in your code using either org.apache.log4j.PropertyConfigurator or org.apache.log4j.xml.DOMConfigurator. Check also, if your third-party code isn't using them.

查看更多
登录 后发表回答