如何在运行时更改log4j2的配置文件?

时间:2013-09-19 22:26:53

标签: java logging configuration log4j log4j2

我想在运行时在log4j2中关闭我的调试语句。根据文档,我们可以做到这一点。我将我的log4j.xml文件保存在默认包中,然后将jar放在其中。因为我无法使用Dlog4j.configuration = / path / to / log4j.xml修改jar我指定的xml文件。但是,这不起作用。我使用file:// uri尝试了所有组合的所有内容,但仍然没有选择xml。

2 个答案:

答案 0 :(得分:4)

指定配置文件位置的系统属性与log4j-1.x不同。 在log4j2中,属性为“log4j.configurationFile”。

因此,如果配置在jar文件中,您将使用:

-Dlog4j.configurationFile=jar:file:///C:/path/to/jarfile.jar!/path/to/log4j2.xml

(我假设您的配置中设置了monitorInterval,如下所示:http://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticReconfiguration

答案 1 :(得分:1)

必须将以下内容添加到配置文件中:

<Configuration monitorInterval="60" >