java 什么时候需要调用 DOMConfigurator.configure 来读取 log4j.xml 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10941922/
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-10-31 03:10:03  来源:igfitidea点击:

When is it necessary to call DOMConfigurator.configure to read a log4j.xml file?

javalog4j

提问by Mike O

I am learning how to use log4j(with slf4j) in a Java project. Some web pages I have read say to call DOMConfigurator.configure("log4j.xml")to incorporate the settings in the log4j.xmlfile into a Loggeryou have created.

我正在学习如何在 Java 项目中使用log4j(带有slf4j)。我读过的一些网页说要调用DOMConfigurator.configure("log4j.xml")log4j.xml文件中的设置合并到Logger您创建的文件中。

I have also seen plenty of code examples where a log4j.xmlfile and some Java code are presented, and the Java code does not call DOMConfigurator.configure. The descriptions around the code samples imply that the log4j.xmlsettings are getting picked up by the logger.

我还看到了很多代码示例,其中提供了一个log4j.xml文件和一些 Java 代码,而 Java 代码不会调用DOMConfigurator.configure. 代码示例周围的描述暗示log4j.xml记录器正在获取这些设置。

In my project, if I don't call DOMConfigurator.configure, the log4j.xmlfile is not read. Are there conventions for when a log4j.xmlfile will be found when getLoggeris called, and loading the file explicitly with DOMConfigurator.configureis not necessary?

在我的项目中,如果我不调用DOMConfigurator.configurelog4j.xml则不会读取文件。是否有关于何时log4j.xmlgetLogger调用时找到文件的约定,并且DOMConfigurator.configure不需要显式加载文件?

回答by MJB

What if log4j.xml wasnt at the root of the class path? That's the convention. If it's at the root of the class path of the current classloader, you don't need to do a thing

如果 log4j.xml 不在类路径的根目录下怎么办?这就是约定。如果它在当前类加载器的类路径的根目录下,则不需要做任何事情

See herefor some information. Also, try running java with -Dlog4.debug (as discussed here)

请参阅此处了解一些信息。另外,尝试使用 -Dlog4.debug 运行 java(如这里所讨论的)