title: Log4j2 default rollover strategy deleting new log files
body: <p>I am trying to configure log4j2 to use a rollingFileAppender but it is not working as I expect it to.</p>
  
  <p>Here is my current code for configuring log4j2 which is a slightly modified version of what is found here: <a href="https://logging.apache.org/log4j/2.x/manual/customconfig.html#Configurator" rel="nofollow">https://logging.apache.org/log4j/2.x/manual/customconfig.html#Configurator</a> under the rollingFileAppender example  ( I have also used the example given there without modifications with similar results)</p>
  
  <pre><code>   LoggerContext ctx;
     public Logger logger;
  
  
      ConfigurationBuilder&lt; BuiltConfiguration &gt; 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");
  </code></pre>
  
  <p>What I expect this to do: log to the <code>rollingTest.log</code> file, then every minute it should archive that into a <code>rollingTest-16-21-07-11-30.log.gz</code> file and then start logging again in the <code>rollingTest.log</code> file, until a rollover happens another minute after that.</p>
  
  <p>What actually happens: Logs get logged to <code>rollingTest.log</code> and after a minute of starting my server the first rollover attempts to happen, but the file is deleted and then after that a rollover attempts to happen every second. At this point if I try to log anything more, it will try to archive it in a new file, but that new file will also be deleted.</p>
  
  <p>Here are the logs of this happening:</p>
  
  <pre><code>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 -&gt; /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 -&gt; /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 -&gt; /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 -&gt; /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]
  </code></pre>
  
  <p>If I put a <code>%i</code> argument into the file name then my new files will not be deleted, but the rollover process still fires every second after the initial one has completed and I end up with a new file for every log event I send to the logger.</p>
  
  <p>Edit: I just tried this using an XML configuration file instead of the programmatic approach above with similar results. Here is the configuration file I used:</p>
  
  <pre><code>&lt;Configuration status="DEBUG"&gt;
  &lt;Appenders&gt;
      &lt;RollingFile name="RollingFile" fileName="target/logfile.log"
                   filePattern="target/archive/logfile-%d{yyyy-MM-dd-hh-mm-ss}.log" &gt;
          &lt;PatternLayout&gt;
              &lt;pattern&gt;%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n&lt;/pattern&gt;
          &lt;/PatternLayout&gt;
          &lt;Policies&gt;
              &lt;CronTriggeringPolicy schedule="0 * * * * ?"/&gt;
          &lt;/Policies&gt;
      &lt;/RollingFile&gt;
  &lt;/Appenders&gt;
  &lt;Loggers&gt;
      &lt;Logger name="root" level="debug" additivity="false"&gt;
          &lt;appender-ref ref="RollingFile" level="debug"/&gt;
      &lt;/Logger&gt;
      &lt;Logger name="TestLogger" level="debug" additivity="false"&gt;
          &lt;appender-ref ref="RollingFile" level="debug"/&gt;
      &lt;/Logger&gt;
      &lt;Root level="debug" additivity="false"&gt;
          &lt;AppenderRef ref="RollingFile"/&gt;
      &lt;/Root&gt;
  &lt;/Loggers&gt;
  </code></pre>
  
  <p></p>
  
  <p>When I had a <code>%i</code> in the file name the rollover triggered every second from the moment my server started and created a new file for each log event sent to it.</p>
  
  <p>When I didn't have a <code>%i</code> in the file name I had two experiences: </p>
  
  <ul>
  <li><p>the first was a rollover every second after the initial event and a new file for every logging event</p></li>
  <li><p>the second was what I described above where my roll over files got deleted after they were created. This also triggered a rollover every second after the initial event.</p></li>
  </ul>