- 附上两个appender配置:RollingRandomAccessFileAppender和自定义的LogStoreAppender
<appenders>
<RollingRandomAccessFile name="UPDATE_FILE" immediateFlush="true"
fileName="${LOG_HOME}/update.log"
filePattern="${LOG_HOME}/update.log.%d{yyyy-MM-dd}">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %class{36} %L %M - %msg%xEx%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
</RollingRandomAccessFile>
<LogStoreAppender name="ARTS_FILE" immediateFlush="true"
fileName="${LOG_HOME}/arts.log"
filePattern="${LOG_HOME}/arts.log.%d{yyyy-MM-dd}">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %class{36} %L %M - %msg%xEx%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
</LogStoreAppender>
</appenders>
- 自定义appender方式
log4j2自定义appener可以直接类似RollingRandomAccessFileAppender,去 extends AbstractOutputStreamAppender<RollingRandomAccessFileManager>
然后只需要在start()和append()中实现自己的逻辑。 -
rollover源码
注意下面RollingRandomAccessFileAppender类中的append()中调用manager.checkRollover(event);去主动check是否需要rollover
我们使用的是TimeBasedTriggeringPolicy