java log4j:使用了哪个配置文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5584687/
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
log4j: which config file was used?
提问by Manic
Using log4j, how can I find out what is the name and path oft the current DOMConfigurator file log4j.xml is used, to reset the file name using the PropertyConfigurator.configureAndWatch method, which needs the name and path of this file to see if it has changed.
使用log4j,如何找出当前使用的DOMConfigurator 文件log4j.xml 的名称和路径,使用PropertyConfigurator.configureAndWatch 方法重置文件名,该方法需要此文件的名称和路径来查看是否已经改变。
The API document shows me how to configure log4j to reload the config, but I cannot find a way to see the filename and path it picked up automatically. The application is running standalone withat any application server.
API 文档向我展示了如何配置 log4j 以重新加载配置,但我找不到一种方法来查看它自动获取的文件名和路径。该应用程序在任何应用程序服务器上独立运行。
Thanks.
谢谢。
回答by FrVaBe
I am afraid you have no chance to get the automatically picked up path from the API. As I understand log4j's source code the detected path will just be used and not be stored.
恐怕您没有机会从 API 中获取自动拾取的路径。据我了解 log4j 的源代码,将只使用检测到的路径而不存储。
At least you can use the -Dlog4j.debug
property to output log4j-internal debugging information on startup and you will get some information like this:
至少你可以使用该-Dlog4j.debug
属性在启动时输出 log4j-internal 调试信息,你会得到一些这样的信息:
log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@11b86e7.
log4j: Using URL [file:/C:/develop/workspace/foobar/target/classes/log4j.xml] for automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
The 'log4j: Using URL ...'
line comes from the LogManagerclass. You can check the initialization process from here. As I see the URL will not be stored for later information.
该'log4j: Using URL ...'
行来自LogManager类。您可以从这里检查初始化过程。正如我所见,该 URL 将不会被存储以供以后使用。
回答by vklidu
You can use same process as log4j use on static initialization at LogManager class. Be aware of other initializations and external configurations e.g. from Spring's org.springframework.web.util.Log4jConfigListener
.
您可以在 LogManager 类的静态初始化中使用与 log4j 相同的过程。注意其他初始化和外部配置,例如来自 Spring 的org.springframework.web.util.Log4jConfigListener
.
public static URL getLog4jConfig() {
String override = OptionConverter.getSystemProperty("log4j.defaultInitOverride", null);
if (override == null || "false".equalsIgnoreCase(override)) {
String configurationOptionStr = OptionConverter.getSystemProperty("log4j.configuration", null);
URL url;
if (configurationOptionStr == null) {
url = Loader.getResource("log4j.xml");
if (url == null) {
url = Loader.getResource("log4j.properties");
}
} else {
try {
url = new URL(configurationOptionStr);
} catch (MalformedURLException ex) {
url = Loader.getResource(configurationOptionStr);
}
}
return url;
} else {
return null;
}
}