2016-07-21 70 views
2

rollingFileAppenderを使用するようにlog4j2を設定しようとしていますが、期待通りに動作しません。ここ Log4j2デフォルトのロールオーバー戦略新しいログファイルを削除する

がここで見出されるもののわずかに変更されたバージョンであるlog4j2構成するための私の現在のコードである:rollingFileAppender例の下 https://logging.apache.org/log4j/2.x/manual/customconfig.html#Configurator

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"); 
(Iも同様の結果を変更することなく、そこに与えられた例を使用しています)私はこれを行うことを期待何

:ロールオーバーはその後、別の分を発生するまで、その後毎分それは、rollingTest-16-21-07-11-30.log.gzファイルにそれをアーカイブしてからrollingTest.logファイルに再びログインし始める必要があり、rollingTest.logファイルにログを記録。

実際に何が起こるか:ログはrollingTest.logに記録され、私のサーバーを1分間起動すると、最初のロールオーバーが実行されますが、ファイルが削除され、その後ロールオーバーが毎秒発生しようとします。この時点で、さらに何かをログに記録しようとすると、新しいファイルにアーカイブしようとしますが、その新しいファイルも削除されます。ここで

はこの出来事のログです:私は、ファイル名に%i引数を置く場合

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] 

は、私の新しいファイルは削除されませんが、ロールオーバープロセスはまだ初期の後に毎秒を発射します私はロガーに送信するすべてのログイベントに対して新しいファイルを作成します。

編集:私はちょうど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を持っていなかった場合は

は、私は2つの経験をしていた:最初は、最初のイベントとすべてのイベントログ

のための新しいファイルの後に毎秒ロールオーバー

  • ました

  • 二つ目は、私は、彼らが作成された後にファイルの上に私のロールを削除してしまったところ、上記の何でした。これはまた、初期イベントの後に1秒ごとにロールオーバを引き起こした。

+0

使用しているLog4j 2のバージョンを指定できますか? –

+0

バージョン2.6.2を使用しています – mBeckham

答えて

関連する問題