Log4J2 - 在运行时分配文件appender文件名 [英] Log4J2 - assigning file appender filename at runtime

查看:995
本文介绍了Log4J2 - 在运行时分配文件appender文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类路径中有一个log4j2.xml配置文件。其中一个appender是File appender,我想在Java应用程序中运行时设置目标文件名。

I have a log4j2.xml config file in the class path. One of the appenders is a File appender, and I would like to set the target file name at run time in the Java application.

根据docs 我应该能够在log4j2.xml中使用双$和上下文前缀档案:

According to the docs I should be able to use a double "$" and a context prefix in the log4j2.xml file:

<appenders>
    <File name="MyFile" fileName="$${sys:logFilename}">
        <PatternLayout pattern="%-4r %d{${datestamp}} [%t] %-5level %logger{36} - %msg%n"/>
    </File>
</appenders>

其中sys前缀表示Configurator将在系统属性中查找属性logFilename 。所以在应用程序中,我调用(相当早期):

where the "sys" prefix indicates that the Configurator will lookup the property "logFilename" in the system properties. So in the application, I call (rather early on):

System.setProperty("logFilename", filename);

我还在xml文件中打开了log4j2的自动重新配置:

I have also turned on auto-reconfiguration for log4j2 in the xml file:

<configuration status="debug" monitorInterval="5">>

不幸的是,这没有任何效果,并且永远不会创建日志文件。一些log4j2状态输出如下:

Unfortunately, this has no effect whatsoever, and the log file is never created. Some of the log4j2 status output is below:


2013-02-13 15:36:37,574 DEBUG在类org.apache上调用createAppender。 logging.log4j.core.appender.FileAppender for element File with params(fileName =$ {sys:logFilename},append =null,locking =null,name =MyFile,immediateFlush =null, suppressExceptions =null,bufferedIO =null,PatternLayout(% - 4r%d {yyyy-MM-dd / HH:mm:ss.SSS / zzz} [%t]%-5level%logger {36} - % msg%n),null)

2013-02-13 15:36:37,574 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.FileAppender for element File with params(fileName="${sys:logFilename}", append="null", locking="null", name="MyFile", immediateFlush="null", suppressExceptions="null", bufferedIO="null", PatternLayout(%-4r %d{yyyy-MM-dd/HH:mm:ss.SSS/zzz} [%t] %-5level %logger{36} - %msg%n), null)

2013-02-13 15:36:37,576 DEBUG启动FileManager $ {sys:logFilename}

2013-02-13 15:36:37,576 DEBUG Starting FileManager ${sys:logFilename}

如何在运行时设置File Appender中的fileName值?或者,我怎样才能简单地添加一个新的在运行时将Appender归档到根记录器?在Log4j 2.0中,大多数用于更改配置的API都是隐藏的。

How can I have the value of "fileName" in the File Appender be set at run time? Alternatively, how can I simply add a new File Appender to the root logger at run time? In Log4j 2.0 most of the API to change the configuration is hidden.

推荐答案

h / t rgoers
FileAppender在文件名上不支持两个美元符号,因为启动appender时会打开文件。您用两个美元符号表示的是您想要的 - 可能 - 每个事件的文件名不同。

h/t rgoers The FileAppender doesn't support two dollar signs on the file name as the file is opened when the appender is started. What you are indicating with two dollar signs is that you want - potentially - a different file name for each event.

使用单个$(如 $ {sys:logFilename} ),系统将在系统属性中查找属性logFilename。

With a single $ (as in ${sys:logFilename}), the system will look for property "logFilename" in the system properties.

因此,log4j2。 xml应该有:

Thus, the log4j2.xml should have:

<appenders>
    <File name="MyFile" fileName="${sys:logFilename}">
        <PatternLayout pattern="%-4r %d{${datestamp}} [%t] %-5level %logger{36} - %msg%n"/>
    </File>
</appenders>

Java应用程序应该设置系统属性:

The Java application should set the system property:

System.setProperty("logFilename", filename);

重新配置记录器:

org.apache.logging.log4j.core.LoggerContext ctx =
    (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
ctx.reconfigure();

这会产生所需的行为。

这篇关于Log4J2 - 在运行时分配文件appender文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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