log4j 干扰 - 级别一直设置为“关闭"; [英] log4j interference - level keeps being set to "OFF"

查看:39
本文介绍了log4j 干扰 - 级别一直设置为“关闭";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中设置 log4j,而我的第三方 JAR 之一似乎以某种方式不断将我的日志级别设置为关闭".它仅在执行该库中的代码时发生.我不知道它是如何做到这一点的,因为我没有使用它知道名称的记录器,而且我没有使用根记录器.我在这上面花了 5 个小时,到目前为止我唯一的成就是获得了超过 9000 的挫败感.

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.

我在 Windows 7 64 位、Java 6 u23 上使用最新的 Tomcat (7.0.37).

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

log4j.properties:

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

然后在我的 Servlet 中进行测试:

then in my Servlet for testing I have:

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

每次运行 DoPost 或 DoGet 时都会运行此代码,并且级别保持在我设置的级别 (TRACE),直到第三方代码运行并将其关闭.

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.

我的问题是:

  1. 其他记录器如何干扰我自己命名和设置的记录器?
  2. 如何阻止这种情况发生,并强制达到我设置的级别?

谢谢:)

推荐答案

  1. Log4j 配置未合并,似乎其他配置存在并被读取而不是您的配置,或者配置被覆盖.在 JAR 中嵌入 log4j 配置文件是许多类似问题的根源.

  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.

您可以使用 org.apache.log4j.PropertyConfiguratororg.apache.log4j.xml.DOMConfigurator 在代码中重新加载配置.还要检查您的第三方代码是否未使用它们.

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.

这篇关于log4j 干扰 - 级别一直设置为“关闭";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆