是否可以让 log4j 显示它用来配置自身的文件?

发布于 2024-09-24 08:03:17 字数 693 浏览 2 评论 0原文

问题

是否可以让Log4J显示用于配置的文件的完整路径?


Background

我对 log4j 是又爱又恨。在好的时候,它很棒,但当它不起作用时,它可能是最难调试的事情之一。我管理应用程序中的所有日志记录。因此,我非常熟悉日志记录和手册中定义的默认初始化过程。< /a> 尽管如此,似乎每隔几周,日志记录就会中断,我会花费大量时间来解决问题。

这一次,已经严重破损了。各地的每一条日志语句都被转储到控制台,我不明白为什么。上周使用我的 log4j.xml 文件的相同代码库突然使用了一些其他配置。没有任何明显的改变。我唯一的猜测是,一些依赖项发生了变化,我怀疑 Maven 下载了一些破坏一切的邪恶 JAR。

如果我能弄清楚Log4J 决定在启动时使用哪个配置文件,我就可以轻松解决这个问题以及大多数其他问题。


Summary

有没有办法告诉 Log4J 打印它用于配置的文件?或者,有没有办法中断正在运行的应用程序并使用调试器来回答这个问题(可能使用表达式或通过检查变量)?

Question

Is it possible to make Log4J display the full path of the file it used for configuration?


Background

I have a love-hate relationship with log4j. In good times, it's great but when it's not working, it can be one of the most difficult things to debug. I manage all the logging in our application. As such, I'm very familiar with logging and the default initialization procedure defined in the manual. Still, it seems that, every few weeks, logging breaks and I spend a lot of time sorting out the problem.

This time, it's severely broken. Every single log statement everywhere is being dumped to the console and I can't figure out why. The same exact code base that used my log4j.xml files last week, is suddenly using some other configuration. Nothing obvious has changed. My only guess is, a few dependencies have changed and I suspect Maven has downloaded some evil JAR that's breaking everything.

If I could just figure out which configuration file Log4J decided to use at startup, I could tackle this and most other problems easily.


Summary

Is there some way to tell Log4J to print which file it used for configuration? Alternatively, is there a way to break a running application and use the debugger to answer this question (maybe with an expression or by inspecting variables)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽清梦 2024-10-01 08:03:17

是的,只需将 log4j.debug 添加到 JVM 系统变量即可。例如:

java -Dlog4j.debug -cp ... some.class.name

Log4j 将输出如下内容:

log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@1f7182c1.  
log4j: Using URL [file:/C:/Users/matt/workspace/projectname/target/test-classes/log4j.xml] for automatic log4j configuration.  
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator  
log4j: System property is :null  
log4j: Standard DocumentBuilderFactory search succeded.  
log4j: DocumentBuilderFactory is: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl  
log4j: debug attribute= "null".  
log4j: Ignoring debug attribute.  
log4j: reset attribute= "false".  
log4j: Threshold ="null".  
...

有关参考,请参阅手册常见问题解答

Yes, simply add log4j.debug to the JVM system variables. For example:

java -Dlog4j.debug -cp ... some.class.name

Log4j will then output something like the following:

log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@1f7182c1.  
log4j: Using URL [file:/C:/Users/matt/workspace/projectname/target/test-classes/log4j.xml] for automatic log4j configuration.  
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator  
log4j: System property is :null  
log4j: Standard DocumentBuilderFactory search succeded.  
log4j: DocumentBuilderFactory is: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl  
log4j: debug attribute= "null".  
log4j: Ignoring debug attribute.  
log4j: reset attribute= "false".  
log4j: Threshold ="null".  
...

For reference see the manual and the FAQ.

爱,才寂寞 2024-10-01 08:03:17

我正在使用 Log4J2,如果您想从 Java 程序内部获取文件,这对我有用:

LoggerContext lContect = LogManager.getContext();
Field f = lContect.getClass().getDeclaredField("configuration");
f.setAccessible(true);
XmlConfiguration iWantThis = (XmlConfiguration) f.get(lContect);
System.out.println("Config File: " + iWantThis.getName());

I am using Log4J2 and if you want to get the file from inside your Java Program this worked for me:

LoggerContext lContect = LogManager.getContext();
Field f = lContect.getClass().getDeclaredField("configuration");
f.setAccessible(true);
XmlConfiguration iWantThis = (XmlConfiguration) f.get(lContect);
System.out.println("Config File: " + iWantThis.getName());
葮薆情 2024-10-01 08:03:17

log4j 2 的等效方法是在配置文件中设置 。这将显示 log4j2 配置文件的加载位置,以及 log4j2 配置过程的其他内部详细信息。默认情况下,状态记录器级别为 WARN,因此您仅在出现问题时看到通知。

如果未正确找到配置文件,您仍然可以通过将系统属性 org.apache.logging.log4j.simplelog.StatusLogger.level 设置为 TRACE 来启用 log4j2 内部状态日志记录。

The log4j 2 equivalent for this is to set <Configuration status="trace"> in the configuration file. This will display the location from where the log4j2 configuration file is loaded, as well as other internal details of the log4j2 configuration process. By default the status logger level is WARN, so you only see notifications when there is a problem.

If the configuration file is not found correctly, you can still enable log4j2 internal status logging by setting system property org.apache.logging.log4j.simplelog.StatusLogger.level to TRACE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文