log4j2:多个appender将相同的输出写入多个文件

时间:2016-04-15 09:45:16

标签: spring spring-batch log4j2

我正在开发一个弹簧批处理项目,该项目有两个批次,每个批次都有一个单独的启动器。 我使用log4j2进行日志记录,使用以下配置:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="30" status="info" xmlns="http://logging.apache.org/log4j/2.0/config">
<properties>
    <property name="patternlayout">%d [%t] %-5level %logger{36} [%method:%line] - %msg%n</property>
    <property name="filename_first_batch">c:/logs/batchs/first_batch.log</property>
    <property name="filename_first_batch-pattern">c:/logs/batchs/second_batch-%d{yyyy-MM-dd}-%i.log.zip</property>
    <property name="filename_second_batch">c:/logs/batchs/first_batch.log</property>
    <property name="filename_second_batch-patten">c:/logs/batchs/second_batch-%d{yyyy-MM-dd}-%i.log.zip</property>
</properties>

<Appenders>
    <!-- the standard-console-appender -->
    <Console name="console" target="SYSTEM_OUT">
        <PatternLayout pattern="${patternlayout}" />
    </Console>

    <!-- appender for the first batch -->
    <RollingFile name="appender_first_batch" fileName="${filename_first_batch}" filePattern="${filename_first_batch-pattern}"
        append="false" bufferedIO="true" immediateFlush="true">
        <PatternLayout pattern="${patternlayout}" />
        <Policies>
            <TimeBasedTriggeringPolicy />
            <SizeBasedTriggeringPolicy size="10 MB" />
        </Policies>
        <DefaultRolloverStrategy max="10" />
    </RollingFile>

    <!-- appender for the second batch -->
    <RollingFile name="appender_seond_batch" fileName="${filename_second_batch}" filePattern="${filename_second_batch-patten}"
        append="false" bufferedIO="true" immediateFlush="true">
        <PatternLayout pattern="${patternlayout}" />
        <Policies>
            <TimeBasedTriggeringPolicy />
            <SizeBasedTriggeringPolicy size="10 MB" />
        </Policies>
        <DefaultRolloverStrategy max="10" />
    </RollingFile>

</Appenders>

<Loggers>
    <Root level="info">
        <AppenderRef ref="console" />
    </Root>

    <!-- first batch logger -->
    <Logger name="com.mycompany.batch.launcher.FirstBatchLauncher" additivity="false" level="ALL">
        <AppenderRef ref="appender_first_batch" level="ALL" />
        <AppenderRef ref="console" />
    </Logger>

    <!-- second batch logger -->
    <Logger name="com.mycompany.batch.SecondBatchLauncher" additivity="false" level="ALL">
        <AppenderRef ref="appender_second_batch" level="ALL" />
        <AppenderRef ref="console" />
    </Logger>

    <logger name="org.springframework" additivity="false">
        <AppenderRef ref="console" />
        <AppenderRef ref="appender_first_batch" level="info" />
        <AppenderRef ref="appender_second_batch" level="info" />
    </logger>

    <logger name="org.hibernate" additivity="false">
        <AppenderRef ref="console" />
        <AppenderRef ref="appender_first_batch" level="info" />
        <AppenderRef ref="appender_second_batch" level="info" />
    </logger>

</Loggers>

当我运行批处理启动器时,我得到正确创建的日志文件, 但是,如果启动了任何批处理启动程序,它会将相同的输出写入另一个文件。 我想要的是每个批处理都将其输出记录到专用文件中。

1 个答案:

答案 0 :(得分:0)

我认为您正在使用脚本启动批处理作业。如果包含-DjobName = batchJobName,其中batchJobName是您要使用的文件的名称,则可以使用$ {sys:batchJobName}来确定文件的名称。