log4j 到 log4j2 自定义 RollingPolicy

log4j to log4j2 custom RollingPolicy

我们有一个在 log4j 中声明的自定义滚动策略,如下所示:

log4j.appender.testing.rollingPolicy=com.custom.appender.TimeBasedRollingPolicy log4j.appender.testing.rollingPolicy.timeToRolloverInSeconds=60 log4j.appender.testing.rollingPolicy.FileNamePattern=/tmp/cdr.log

如何在 log4j2.xml 中声明?

Log4j2 有一个 built-in time based rollover policy 可以满足您的需求。以下配置导致每分钟翻转一次:

<Appenders>
  <RollingFile name="RollingFile" fileName="logs/app.log"
               filePattern="logs/old/app-%d{yyyyMMdd-HHmm}-log.gz">
    <PatternLayout>
      <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
    </PatternLayout>
    <Policies>
      <TimeBasedTriggeringPolicy />
    </Policies>
  </RollingFile>

如果您想创建自定义翻转策略,您需要创建一个实现 TriggeringPolicy. A good starting point would be to look at the source code for the built-in TimeBasedTriggeringPolicy. General information on Log4j custom plugins is here.

的 log4j2 插件