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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3752921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:25:08  来源:igfitidea点击:

Is it possible to make log4j display which file it used to configure itself?

javalog4j

提问by gMale

Question

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

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



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 lotof time sorting out the problem.

我与 log4j 有爱恨交加的关系。在好的时候,它很好,但当它不工作时,它可能是最难调试的事情之一。我管理我们应用程序中的所有日志记录。因此,我非常熟悉手册中定义的日志记录和默认初始化过程。尽管如此,似乎每隔几周,日志就会中断,我花了很多时间来解决问题。

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.

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

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

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



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

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

采纳答案by matt b

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

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

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

Log4j will then output something like the following:

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".  
...

For reference see the manualand the FAQ.

如需参考,请参阅手册常见问题解答

回答by Ben

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

我正在使用 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());

回答by Remko Popma

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.

与此等效的 log4j 2 是<Configuration status="trace">在配置文件中进行设置。这将显示加载 log4j2 配置文件的位置,以及 log4j2 配置过程的其他内部细节。默认情况下,状态记录器级别为 WARN,因此您只会在出现问题时看到通知。

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.levelto TRACE.

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