2016-04-15 282 views
0

我正在開發一個批處理項目,該項目有兩個批次,每個批處理都有一個單獨的啓動器。 我使用log4j2與以下配置日誌:log4j2:多個appender將相同的輸出寫入多個文件

<?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> 

當我運行批處理啓動,我得到的日誌文件創建的正確,但是 如果任何批次發射器啓動時,它將相同的輸出寫入其他文件。 我想要的是每個批次都將其輸出記錄到專用文件中。

回答

0

我認爲您正在使用腳本啓動批處理作業。如果包含-DjobName = batchJobName其中batchJobName是要使用的文件的名稱,則可以使用$ {sys:batchJobName}來確定文件的名稱。

+0

文件名已在log4j2配置中指定,此外,我已經嘗試了您的建議,但它不起作用,我仍然將相同的輸出日誌寫入其他文件 – bolbol

相關問題