title: how to retain logs for last 50 days only in log4j
body: <p>I have requirement to retain logs for last 50 days only (i only want logs for last 50 days only). in the log4j properties file i have maxbackup index set it to 1 for testing purposes. i am aware that log4j dailyrollingfileappender only rollover monthly yearly daily etc.
  But my requirement is to roll over every day but only want for last 50 days, rest to be deleted.
  So i have found CustomAppender which extends which extends daily rollingfileappender and my customappender looks as below. Basically we have added maxbackupindex to daily rollingfile appender.</p>
  
  <pre><code>Code:
      import java.io.File;
      import java.io.IOException;
      import java.util.ArrayList;
      import java.util.Arrays;
      import java.util.List;
  
      import org.apache.log4j.DailyRollingFileAppender;
      import org.apache.log4j.helpers.LogLog;
  
      public class CustomAppender extends DailyRollingFileAppender {
  
          private int maxBackupIndex;
  
          public void setMaxBackupIndex(int maxBackupIndex) {
              this.maxBackupIndex = maxBackupIndex;
          }
  
          @Override
          public void rollOver() throws IOException {
              super.rollOver();
              File file = new File(fileName);
              List&lt;String&gt; files = new ArrayList&lt;&gt;();
              File[] dirFiles = new File(file.getAbsolutePath()).getParentFile().listFiles();
              if (dirFiles != null &amp;&amp; dirFiles.length &gt; 0) {
                  Arrays.sort(dirFiles, (a, b) -&gt; Long.compare(a.lastModified(), b.lastModified()));
                  for (File allFile : dirFiles) {
                      if (allFile.getName().contains(fileName)) {
                          files.add(allFile.getAbsolutePath());
                      }
                  }
              }
              if (files.size() &gt; maxBackupIndex+1) {
                  File deleteFile = new File(files.get(0));
                  LogLog.debug("delete result for"+deleteFile.getAbsolutePath()+" is "+deleteFile.delete());
                  files.remove(0);
              }
  
          }
      }        
  
  
      my log4j properties looks below.
  
      log4j.rootLogger=WARN,stdout
      log4j.logger.X.Y.Z.A=DEBUG,Application
      log4j.appender.Application=org.apache.log4j.CustomAppender
      log4j.appender.Application.File=/h/i/logs/m/Application.log
      log4j.appender.Application.MaxBackupIndex=1
      log4j.appender.Application.layout=org.apache.log4j.PatternLayout
      log4j.appender.Application.layout.ConversionPattern=%d{DATE} %-5p %m - %c [%t] [%r ms]%n%n
      #set up the apache appender
      log4j.logger.org.apache=ON
      # Hibernate loggers and appender
      #log4j.logger.org.hibernate=DEBUG,Hibernate
      log4j.logger.org.hibernate=DEBUG,Hibernate
      log4j.additivity.org.hibernate=false
      log4j.logger.org.hibernate.SQL=DEBUG,Hibernate
      log4j.additivity.org.hibernate.SQL=false
      log4j.logger.org.hibernate.type=DEBUG,Hibernate
      log4j.additivity.org.hibernate.type=false
      log4j.appender.Hibernate=org.apache.log4j.CustomAppender
      log4j.appender.Hibernate.File=/h/i/logs/m/Hibernate.log
      log4j.appender.Hibernate.MaxBackupIndex=1
      log4j.appender.Hibernate.layout=org.apache.log4j.PatternLayout
      log4j.appender.Hibernate.layout.ConversionPattern=Hib: %d [%t] %-5p %c - %m%n%n
  
      However, the previous files are not being deleted , even though maxbackupindex is set to 1.
      any one can please help me out?
  </code></pre>