java 如何强制 log4j 从非 src 文件夹加载 xml 配置?

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

How to force log4j load xml config from non src folder?

javaxmllog4j

提问by Denys S.

Is it possible to have log4j.xml loaded from a different dir than source root and how? (programmatically?) Meaning that it's somewhere in FS not just in classpath.

是否可以从与源根目录​​不同的目录加载 log4j.xml 以及如何加载?(以编程方式?)意味着它在 FS 中的某个地方,而不仅仅是在类路径中。

回答by Mark

Using DOMConfiguratoryou can specify the the XML file used to configure log4j.

使用DOMConfigurator您可以指定用于配置 log4j 的 XML 文件。

DOMConfigurator.configure("/path/to/log4j.xml");

For log4j.properties you can do the same thing with PropertyConfigurator.

对于 log4j.properties,您可以使用PropertyConfigurator做同样的事情。

PropertyConfigurator.configure("/path/to/log4j.properties");

回答by Jon7

This might not be the best way, but you can specify the location of the file by setting the java property log4j.configuration=path/to/config/file. For example, you can specify this directly to the java command with java -Dlog4j.configuration=path.

这可能不是最好的方法,但您可以通过设置 java 属性 log4j.configuration=path/to/config/file 来指定文件的位置。例如,您可以使用java -Dlog4j.configuration=path.