I have been trying to archive my application logs file which are older than a certain period. Noticed that since log4j 2.5 we have a Delete
tag which let's you define the criteria based on which we can delete/archive our logs. Tried using this but I am somehow not able to crack it. Tried with a 30day 30d
value and that isn't working on my server and neither is a 20 second policy PT20S
working in my Dev Machine.
Any direction is greatly appreciated.
XML is as below:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyyMMdd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RollingFile name="RollingFile" fileName="C://logs///test.log" filePattern="C://logs//test-%d{MM-dd-yyyy}.log.gz" ignoreExceptions="false">
<PatternLayout pattern="%d{yyyyMMdd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
<TimeBasedTriggeringPolicy />
<DefaultRolloverStrategy>
<Delete basePath="C://logs//" maxDepth="2">
<IfFileName glob="*/test-*.log.gz" />
<IfLastModified age="PT20S" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
If you're using Windows the file and filePattern of the RollingFile appender can use single slashes. The double slashes may confuse it.
You can debug by setting <Configuration status="trace">
in the beginning of the configuration file.
Update:
The rolled over files end up in the c:/logs directory (filePattern="C://logs//test-%d{MM-dd-yyyy}.log.gz"
).
However, the Delete action is configured to only look at files ending in "log.gz" that are in subdirectories of c:/logs. Files in the c:/logs directory itself are not matched by glob="*/test-*.log.gz"
.
To fix this, use glob="test-*.log.gz"
.
It was mentioned in the comments that changing glob to regex also resolved the problem.