Log4j2 RollingFile每日轮换

时间:2017-02-13 14:13:31

标签: log4j2

使用Log4j2 2.8,有没有办法让我的日志每天轮换,但是当前文件有一个常量名?

示例:

my.log <-- current day (2017-02-13)
my-2017-02-12.log <-- last log
my-2017-02-11.log <-- second to last log

我尝试了以下配置但没有成功:

<RollingFile name="RollingFileMain" fileName="my.log"
             filePattern="my-%d{yyyy-MM-dd}-%i.log">
    <PatternLayout>
        <Pattern>%level{length=1} %d{yyyy-MM-dd HH:mm:ss} %c{-3} (%F:%L) %m%n</Pattern>
    </PatternLayout>
    <Policies>
        <TimeBasedTriggeringPolicy/>
    </Policies>
    <DefaultRolloverStrategy max="2"/>
</RollingFile>

此外,当我在某一天第一次启动我的应用程序时,我收到以下异常:

2017-02-14 09:28:38,334 main ERROR An exception occurred processing Appender RollingFileMain java.lang.NullPointerException
    at org.apache.logging.log4j.core.appender.rolling.AbstractRolloverStrategy.getEligibleFiles(AbstractRolloverStrategy.java:107)
    at org.apache.logging.log4j.core.appender.rolling.AbstractRolloverStrategy.getEligibleFiles(AbstractRolloverStrategy.java:96)
    at org.apache.logging.log4j.core.appender.rolling.AbstractRolloverStrategy.getEligibleFiles(AbstractRolloverStrategy.java:89)
    at org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy.purgeAscending(DefaultRolloverStrategy.java:212)
    at org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy.purge(DefaultRolloverStrategy.java:199)
    at org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy.rollover(DefaultRolloverStrategy.java:326)
    at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.rollover(RollingFileManager.java:310)
    at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.rollover(RollingFileManager.java:227)
    at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.checkRollover(RollingFileManager.java:207)
    at org.apache.logging.log4j.core.appender.RollingFileAppender.append(RollingFileAppender.java:267)
    at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
    at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:448)
    at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:433)
    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:417)
    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:403)
    at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)
    at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2091)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:1988)
    at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1960)
    at org.apache.logging.log4j.spi.AbstractLogger.info(AbstractLogger.java:1297)
    at my.package.MainKt.main(Main.kt:48)

2 个答案:

答案 0 :(得分:0)

请将您的文件模式更改为filePattern="my-%d{yyyy-MM-dd}.log"

%i仅在使用SizeBasedTriggeringPolicy时有用。如果你只有一个TimeBasedTriggeringPolicy,Log4j会被%i弄糊涂......

答案 1 :(得分:0)

问题似乎是@Remko Popma的答案和Log4j2 2.8 中的错误的组合。应用filePattern="my-%d{yyyy-MM-dd}.log"并降级到版本 2.7 后,一切都按预期工作。

我在此处提交了有关崩溃的问题:https://issues.apache.org/jira/browse/LOG4J2-1815