如何指定 Log4J 2.x 配置位置? [英] How to specify Log4J 2.x config location?

查看:28
本文介绍了如何指定 Log4J 2.x 配置位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法手动指定 Log4J 2.x log4j2.xml 文件位置(如 Log4J 1.x 中的 DOMConfigurator),而不会弄乱类路径和系统属性?

Is there any way to specify Log4J 2.x log4j2.xml file location manually (like DOMConfigurator in Log4J 1.x), without messing with classpath and system properties?

推荐答案

您可以使用静态方法 #initialize(String contextName, ClassLoader loader, String configLocation)(请参阅源代码 此处)在<代码中>org.apache.logging.log4j.core.config.Configurator.(您可以为类加载器传递 null.)

You could use the static method #initialize(String contextName, ClassLoader loader, String configLocation) (see source here) in org.apache.logging.log4j.core.config.Configurator. (You can pass null for the class loader.)

请注意,此类不是公共 API 的一部分,因此您的代码可能会因任何次要版本而中断.

Be aware that this class is not part of the public API so your code may break with any minor release.

为了完整起见,您还可以使用此系统属性指定配置文件的位置:

For completeness, you can also specify the location of the configuration file with this system property:

-Dlog4j.configurationFile=path/to/log4j2.xml

这篇关于如何指定 Log4J 2.x 配置位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆