log4j.xml中的参数化附加程序

时间:2018-09-24 06:52:22

标签: log4j appender

我在log4j.xml中有多个追加程序,唯一的区别是文件路径,如下面的代码所示。

<appender name="Thread_1" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="file"              value="Log/Thread_1.log" />
    <param name="DatePattern"       value="'.'yyyy-MM-dd-HH" />
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="[%d{HH:mm:ss}][%-5p][%-20F:%3L] %m%n" />
    </layout>
</appender>

<appender name="Thread_2" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="file"              value="Log/Thread_2.log" />
    <param name="DatePattern"       value="'.'yyyy-MM-dd-HH" />
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="[%d{HH:mm:ss}][%-5p][%-20F:%3L] %m%n" />
    </layout>
</appender>

<appender name="Thread_3" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="file"              value="Log/Thread_3.log" />
    <param name="DatePattern"       value="'.'yyyy-MM-dd-HH" />
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="[%d{HH:mm:ss}][%-5p][%-20F:%3L] %m%n" />
    </layout>
</appender>

<logger name="com.testapp.task.Thread_1" additivity="false">
    <level value="INFO" />
    <appender-ref ref="Thread_1" />
</logger>

<logger name="com.testapp.task.Thread_2" additivity="false">
    <level value="INFO" />
    <appender-ref ref="Thread_2" />
</logger>

<logger name="com.testapp.task.Thread_3" additivity="false">
    <level value="INFO" />
    <appender-ref ref="Thread_3" />
</logger>

所以,我想知道是否有办法只写一个带有文件路径作为参数的追加程序,例如(以下伪代码)

<logger name="com.testapp.task.Thread_3" additivity="false">
    <level value="INFO" />
    <appender-ref ref="Thread_common" file="Log/Thread_1.log" />
</logger>

关于, 布莱恩

0 个答案:

没有答案