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
When is it necessary to call DOMConfigurator.configure to read a log4j.xml file?
提问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.xml
file into a Logger
you have created.
我正在学习如何在 Java 项目中使用log4j(带有slf4j)。我读过的一些网页说要调用DOMConfigurator.configure("log4j.xml")
将log4j.xml
文件中的设置合并到Logger
您创建的文件中。
I have also seen plenty of code examples where a log4j.xml
file 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.xml
settings 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.xml
file is not read. Are there conventions for when a log4j.xml
file will be found when getLogger
is called, and loading the file explicitly with DOMConfigurator.configure
is not necessary?
在我的项目中,如果我不调用DOMConfigurator.configure
,log4j.xml
则不会读取文件。是否有关于何时log4j.xml
在getLogger
调用时找到文件的约定,并且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)