在log4j 1.2到log4j 2的迁移中,DailyRollingFileAppenderclass怎么办?

In log4j 1.2 to log4j 2 migration, what to do with the DailyRollingFileAppender class?

我正在努力将 Java 项目从使用 log4j 1.2 进行日志记录迁移到使用 log4j 2。

log4j 1.x 有一个 class org.apache.log4j.DailyRollingFileAppender 在我项目的 log4j.properties 配置文件中提到。 log4j 2 中不再存在同名 DailyRollingFileAppender 的 class。我想知道 DailyRollingFileAppender 的角色是否可以被其他 class(es) 或某些替代品取代log4j 2 中的配置或实现。

迁移指南强调应用程序不得访问 log4j 1.x 内部的方法和 classes,例如 Appenders 才能迁移到 log4j 2。但是在为了进行迁移,可以对 DailyRollingFileAppender 做些什么?是否可以通过一些自定义配置或 log4j 2 中 classes 的组合来替换它?有 some configuration examples in the migration guide,特别是 FileAppender,但是 DailyRollingFileAppender 呢?这与基本 FileAppender:

相当接近

来自 log4j 1.2 API Javadoc、class org.apache.log4j.DailyRollingFileAppender

DailyRollingFileAppender extends FileAppender so that the underlying file is rolled over at a user chosen frequency. DailyRollingFileAppender has been observed to exhibit synchronization issues and data loss. The log4j extras companion includes alternatives which should be considered for new deployments and which are discussed in the documentation for org.apache.log4j.rolling.RollingFileAppender.

您正在寻找 RollingFile appender

<RollingFile name="DAILY_LOG" fileName="log/daily.log"
                 filePattern="log/%d{ddMMyyyy}_daily.log"
                 >
      <PatternLayout pattern="%d [%7r] %5p - %c - %m%n"/>
      <Policies>
        <TimeBasedTriggeringPolicy interval="1"/>
      </Policies>                               
</RollingFile>

前一个样本按天滚动,间隔为1个单位,由文件模式中日期查找的最小单位决定。换句话说,如果日期模式是 d{MMyyyy},那么 interval=1 对应于 1 个月的滚动周期。

请注意,目前不支持限制保留超过特定日期或时间等的日志文件的数量。您只能限制每个时间段的日志文件数量。