Log4j2默认翻转策略删除新日志文件

时间:2016-07-21 20:47:43

标签: java logging log4j2

我正在尝试将log4j2配置为使用rollingFileAppender,但它无法正常工作。

这是我目前的配置log4j2的代码,这是一个稍微修改过的版本,在这里可以找到:https://logging.apache.org/log4j/2.x/manual/customconfig.html#Configurator在rollingFileAppender示例下(我也使用了那里给出的示例而没有修改,结果类似)

   LoggerContext ctx;
   public Logger logger;


    ConfigurationBuilder< BuiltConfiguration > builder =
    ConfigurationBuilderFactory.newConfigurationBuilder();

    builder.setStatusLevel(Level.ALL);
    builder.setConfigurationName("RollingBuilder");
// create the console appender
    AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target",
            ConsoleAppender.Target.SYSTEM_OUT);
    appenderBuilder.add(builder.newLayout("PatternLayout").
            addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"));
    builder.add( appenderBuilder );

    LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout")
            .addAttribute("pattern", "%d [%t] %-5level: %msg%n");
    ComponentBuilder triggeringPolicy = builder.newComponent("Policies")
            .addComponent(builder.newComponent("CronTriggeringPolicy").addAttribute("schedule", "0 * * * * ?"))
            .addComponent(builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size", "100M"));
    ComponentBuilder defaultStrat = builder.newComponent("DefaultRolloverStrategy")
            .addAttribute("min", 10)
            .addAttribute("max", 20);
    appenderBuilder = builder.newAppender("rolling", "RollingFile")
            .addAttribute("fileName", "targetTest/rollingTEST.log")
            .addAttribute("filePattern", "targetTest/archive/rollingTEST-%d{yy-dd-MM-kk-mm}-test.log.gz") 
            .addAttribute("immediateFlush", false)
            .addComponent(defaultStrat)
            .add(layoutBuilder)
            .addComponent(triggeringPolicy);
    builder.add(appenderBuilder);


// create the new logger
    builder.add( builder.newLogger( "TestLogger", Level.ALL )
            .add( builder.newAppenderRef( "rolling" ) )   );
            //.addAttribute( "additivity", false ) );

    builder.add( builder.newRootLogger( Level.ALL )
            .add( builder.newAppenderRef( "rolling" ) ) );


    this.ctx = Configurator.initialize(builder.build());

    this.logger = ctx.getLogger("TestLogger");

我希望这样做:登录rollingTest.log文件,然后每分钟将其归档到rollingTest-16-21-07-11-30.log.gz文件中,然后再次开始在rollingTest.log文件中记录,直到在那之后的另一分钟发生了翻转。

实际发生的情况:日志记录到rollingTest.log,在启动我的服务器一分钟后,第一次翻转尝试发生,但文件被删除,然后翻转尝试每秒发生一次。此时,如果我尝试记录更多内容,它将尝试将其存档在新文件中,但该新文件也将被删除。

以下是发生这种情况的日志:

2016-07-21 15:20:00,009 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 6.0 milliseconds
2016-07-21 15:20:00,030 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]
2016-07-21 15:20:00,042 Log4j2-Log4j2Scheduled-1 TRACE Renamed file /Users/Source/targetTest/rollingTEST.log to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log with Files.move
2016-07-21 15:20:00,042 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing async GzCompressAction[targetTest/archive/rollingTEST-16-21-07-15-20-test.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz, deleteSource=true]
2016-07-21 15:20:01,004 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:01,004 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-19-test.log, renameEmptyFiles=false]
2016-07-21 15:20:02,004 Log4j2-Log4j2Scheduled-1 DEBUG DefaultRolloverStrategy.purgeAscending deleting targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz at low index 10: all slots full.
2016-07-21 15:20:02,004 Log4j2-Log4j2Scheduled-1 DEBUG DefaultRolloverStrategy.purgeAscending executing 9 of 10: FileRenameAction[targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz, renameEmptyFiles=true]
2016-07-21 15:20:02,005 Log4j2-Log4j2Scheduled-1 ERROR Unable to move file /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz: java.nio.file.NoSuchFileException /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz -> /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz
2016-07-21 15:20:02,006 Log4j2-Log4j2Scheduled-1 ERROR Unable to copy file /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz: java.nio.file.NoSuchFileException /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz
2016-07-21 15:20:03,004 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:03,004 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]
2016-07-21 15:20:04,004 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:04,005 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]
2016-07-21 15:20:05,006 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:05,006 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]
2016-07-21 15:20:06,005 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:06,006 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]
2016-07-21 15:20:06,007 Log4j2-Log4j2Scheduled-1 TRACE Renamed file /Users/Source/targetTest/rollingTEST.log to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log with Files.move
2016-07-21 15:20:06,007 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing async GzCompressAction[targetTest/archive/rollingTEST-16-21-07-15-20-test.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz, deleteSource=true]
2016-07-21 15:20:07,001 Log4j2-Log4j2Scheduled-1 DEBUG DefaultRolloverStrategy.purgeAscending deleting targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz at low index 10: all slots full.
2016-07-21 15:20:07,002 Log4j2-Log4j2Scheduled-1 DEBUG DefaultRolloverStrategy.purgeAscending executing 9 of 10: FileRenameAction[targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz, renameEmptyFiles=true]
2016-07-21 15:20:07,002 Log4j2-Log4j2Scheduled-1 ERROR Unable to move file /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz: java.nio.file.NoSuchFileException /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz -> /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz
2016-07-21 15:20:07,002 Log4j2-Log4j2Scheduled-1 ERROR Unable to copy file /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz: java.nio.file.NoSuchFileException /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz
2016-07-21 15:20:08,003 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:08,004 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]
2016-07-21 15:20:08,005 Log4j2-Log4j2Scheduled-1 TRACE Renamed file /Users/Source/targetTest/rollingTEST.log to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log with Files.move
2016-07-21 15:20:08,005 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing async GzCompressAction[targetTest/archive/rollingTEST-16-21-07-15-20-test.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz, deleteSource=true]
2016-07-21 15:20:09,007 Log4j2-Log4j2Scheduled-1 DEBUG DefaultRolloverStrategy.purgeAscending deleting targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz at low index 10: all slots full.
2016-07-21 15:20:09,009 Log4j2-Log4j2Scheduled-1 DEBUG DefaultRolloverStrategy.purgeAscending executing 9 of 10: FileRenameAction[targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz, renameEmptyFiles=true]
2016-07-21 15:20:09,009 Log4j2-Log4j2Scheduled-1 ERROR Unable to move file /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz: java.nio.file.NoSuchFileException /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz -> /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz
2016-07-21 15:20:09,010 Log4j2-Log4j2Scheduled-1 ERROR Unable to copy file /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz: java.nio.file.NoSuchFileException /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz
2016-07-21 15:20:10,004 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:10,004 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]
2016-07-21 15:20:11,004 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:11,005 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]
2016-07-21 15:20:11,006 Log4j2-Log4j2Scheduled-1 TRACE Renamed file /Users/Source/targetTest/rollingTEST.log to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log with Files.move
2016-07-21 15:20:11,006 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing async GzCompressAction[targetTest/archive/rollingTEST-16-21-07-15-20-test.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz, deleteSource=true]
2016-07-21 15:20:12,006 Log4j2-Log4j2Scheduled-1 DEBUG DefaultRolloverStrategy.purgeAscending deleting targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz at low index 10: all slots full.
2016-07-21 15:20:12,006 Log4j2-Log4j2Scheduled-1 DEBUG DefaultRolloverStrategy.purgeAscending executing 9 of 10: FileRenameAction[targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz, renameEmptyFiles=true]
2016-07-21 15:20:12,007 Log4j2-Log4j2Scheduled-1 ERROR Unable to move file /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz: java.nio.file.NoSuchFileException /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz -> /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz
2016-07-21 15:20:12,007 Log4j2-Log4j2Scheduled-1 ERROR Unable to copy file /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz to /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz: java.nio.file.NoSuchFileException /Users/Source/targetTest/archive/rollingTEST-16-21-07-15-20-test.log.gz
2016-07-21 15:20:13,004 Log4j2-Log4j2Scheduled-1 TRACE DefaultRolloverStrategy.purge() took 0.0 milliseconds
2016-07-21 15:20:13,004 Log4j2-Log4j2Scheduled-1 DEBUG RollingFileManager executing synchronous FileRenameAction[targetTest/rollingTEST.log to targetTest/archive/rollingTEST-16-21-07-15-20-test.log, renameEmptyFiles=false]

如果我在文件名中加入%i参数,那么我的新文件将不会被删除,但翻译过程仍然会在初始文件完成后每秒触发一次,并且每次最终都会有一个新文件我发送给记录器的日志事件。

编辑:我刚尝试使用XML配置文件而不是上面的编程方法,但结果相似。这是我使用的配置文件:

<Configuration status="DEBUG">
<Appenders>
    <RollingFile name="RollingFile" fileName="target/logfile.log"
                 filePattern="target/archive/logfile-%d{yyyy-MM-dd-hh-mm-ss}.log" >
        <PatternLayout>
            <pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n</pattern>
        </PatternLayout>
        <Policies>
            <CronTriggeringPolicy schedule="0 * * * * ?"/>
        </Policies>
    </RollingFile>
</Appenders>
<Loggers>
    <Logger name="root" level="debug" additivity="false">
        <appender-ref ref="RollingFile" level="debug"/>
    </Logger>
    <Logger name="TestLogger" level="debug" additivity="false">
        <appender-ref ref="RollingFile" level="debug"/>
    </Logger>
    <Root level="debug" additivity="false">
        <AppenderRef ref="RollingFile"/>
    </Root>
</Loggers>

当我在文件名中有%i时,从我的服务器启动时起每秒触发一次翻转,并为发送给它的每个日志事件创建一个新文件。

当我在文件名中没有%i时,我有两次经历:

  • 第一个是初始事件后每秒翻转一次,每个记录事件都有一个新文件

  • 第二个是我上面描述的,我的翻转文件在创建后被删除了。这也在初始事件后每秒触发翻转。

1 个答案:

答案 0 :(得分:0)

这似乎是在最新版本中解决的。相关问题, https://localhost:443/webinterface/webui

https://issues.apache.org/jira/browse/LOG4J2-1480

我已更新到2.8.2版本,不再看到此错误。