Log4j2 - Configure RolloverStrategy to delete old log files
<p>I'm trying to configure <em>log4j</em> to keep only specified amount of backup files or keep files that are not older than some age. Ultimately I want to have <em>time [daily] based triggering policy</em> and keep 30 backup files or delete files that are older then 30 days. </p>

<p>After doing some research I learned that I can't specify max number of backup files when using <em>time policy</em> however I came across this issue <a href="https://issues.apache.org/jira/browse/LOG4J2-435" rel="nofollow">https://issues.apache.org/jira/browse/LOG4J2-435</a> and this documentation fragment <a href="http://logging.apache.org/log4j/2.x/manual/appenders.html#CustomDeleteOnRollover" rel="nofollow">http://logging.apache.org/log4j/2.x/manual/appenders.html#CustomDeleteOnRollover</a> that describes how to delete files based on their age and name pattern. Now I'm trying to apply this configuration in simple example that will create new backup file every minute and will automatically delete files that are older then 3 minutes. To do this I created simple maven project with following pom:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;test&lt;/groupId&gt;
    &lt;artifactId&gt;test&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
            &lt;artifactId&gt;log4j-api&lt;/artifactId&gt;
            &lt;version&gt;2.5&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
            &lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
            &lt;version&gt;2.5&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;id&gt;copy-dependencies&lt;/id&gt;
                        &lt;phase&gt;prepare-package&lt;/phase&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;copy-dependencies&lt;/goal&gt;
                        &lt;/goals&gt;
                        &lt;configuration&gt;
                            &lt;outputDirectory&gt;${project.build.directory}/lib&lt;/outputDirectory&gt;
                            &lt;overWriteReleases&gt;false&lt;/overWriteReleases&gt;
                            &lt;overWriteSnapshots&gt;false&lt;/overWriteSnapshots&gt;
                            &lt;overWriteIfNewer&gt;true&lt;/overWriteIfNewer&gt;
                        &lt;/configuration&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
            &lt;/plugin&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
                &lt;configuration&gt;
                    &lt;archive&gt;
                        &lt;manifest&gt;
                            &lt;addClasspath&gt;true&lt;/addClasspath&gt;
                            &lt;classpathPrefix&gt;lib/&lt;/classpathPrefix&gt;
                            &lt;mainClass&gt;test.Main&lt;/mainClass&gt;
                        &lt;/manifest&gt;
                    &lt;/archive&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;
</code></pre>

<p>log4j2.xml config file:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;Configuration status="warn" name="MyApp" packages=""&gt;
    &lt;Appenders&gt;
        &lt;RollingFile name="RollingFile" fileName="D:/app.log"
                     filePattern="D:/app-%d{yyyy-MM-dd-HH-mm-ss}.log"&gt;
            &lt;PatternLayout&gt;
                &lt;Pattern&gt;%d %p %c{1.} [%t] %m%n&lt;/Pattern&gt;
            &lt;/PatternLayout&gt;
            &lt;Policies&gt;
                &lt;TimeBasedTriggeringPolicy interval="60" modulate="true"/&gt;
                &lt;!--&lt;SizeBasedTriggeringPolicy size="250 MB"/&gt;--&gt;
            &lt;/Policies&gt;
            &lt;DefaultRolloverStrategy&gt;
                &lt;Delete basePath="D:" maxDepth="1"&gt;
                    &lt;IfFileName glob="app-*.log" /&gt;
                    &lt;IfLastModified age="3m" /&gt;
                &lt;/Delete&gt;
            &lt;/DefaultRolloverStrategy&gt;
        &lt;/RollingFile&gt;
    &lt;/Appenders&gt;
    &lt;Loggers&gt;
        &lt;Root level="trace"&gt;
            &lt;AppenderRef ref="RollingFile"/&gt;
        &lt;/Root&gt;
    &lt;/Loggers&gt;
&lt;/Configuration&gt;
</code></pre>

<p>and Main class:</p>

<pre><code>package test;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * Created by mzurawski on 2015-12-22.
 */
public class Main {

    final static Logger logger = LogManager.getLogger(Main.class);

    public static void main(String[] args) {
        runMe("test");
    }

    private static void runMe(String parameterParam){
        String parameter;
        for(int i=0; i&lt;100; ++i) {
            parameter = parameterParam + i;
            System.out.println("log iteration: "+i);
            if (logger.isDebugEnabled()) {
                logger.debug("This is debug : " + parameter);
            }

            if (logger.isInfoEnabled()) {
                logger.info("This is info : " + parameter);
            }

            logger.warn("This is warn : " + parameter);
            logger.error("This is error : " + parameter);
            logger.fatal("This is fatal : " + parameter);
            try {
                Thread.sleep(60000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
</code></pre>

<p>after building this program you can run it with following command (watch out for hard coded disc locations):</p>

<pre><code>java -jar test-1.0-SNAPSHOT.jar
</code></pre>

<p>So far the log files are created every minute but no old files are being deleted. How can I get this test configuration to work? How should I modify this configuration so that ultimately only files from last 30 days will be kept?</p>

<p>Thanks for any help.</p>