我使用 log back 实现了这一点,解决方案与 Spring Batch 无关,但与 Spring Boot 无关。
首先,我在 logback 配置文件中为每个作业定义了单独的 Logger,如下所示,
`
<include resource="org/springframework/boot/logging/logback/base.xml" />
<property name="LOG_PATH" value="${LOG_DIR}" />
<timestamp key="timestamp-by-second" datePattern="yyyyMMdd'T'HHmmss" />
<appender name="Console-Appender" class="ch.qos.logback.core.ConsoleAppender">
<layout>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n
</pattern>
</layout>
</appender>
<appender name="Job1"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/Job1.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${LOG_PATH}/Job1.%i.log
</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>600</maxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>5MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n
</pattern>
<outputPatternAsHeader>true</outputPatternAsHeader>
</encoder>
</appender>
<appender name="Job2"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/Job2.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${LOG_PATH}/Job2.%i.log
</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>600</maxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>5MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n
</pattern>
</encoder>
</appender>
<logger name="Job1Logger" level="info" additivity="false">
<appender-ref ref="Job1" />
</logger>
<logger name="Job2erLogger" level="info" additivity="false">
<appender-ref ref="Job2" />
</logger>
<root>
<appender-ref ref="Console-Appender" />
</root>
`
然后代替 - private static final Logger logger = LoggerFactory.getLogger(Job1.class);
,你需要做,private static final Logger logger = LoggerFactory.getLogger("Job1Logger")