I use log4j2 to log my programs. In my xml configuration file, I have this appender:
<RollingFile name="General" fileName="log/logs/giornale.log" filePattern="log/logs/log-%d{yyyyMMdd}.log">
<PatternLayout>
<Pattern>%d{HH:mm:ss,SSS} [%t] %-5level %logger{-1} - %msg%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
What I need is a way to configure it, so that it will delete automatically all files older than n days.
I already found some questions like this, but they don't help, since they don't say how to do it via xml configuration.
To make it short, where exactly am I suppose to indicate parameters like "MaxBackupIndex" in the above snippet? Or which other parameter should I use (and where can I put it)?
Add the following tag under 'RollingFile' tag. Remove the 'policies' tag. You probably don't need it.
<DefaultRolloverStrategy>
<Delete basePath="log/logs" maxDepth="2">
<IfLastModified age="60d" />
</Delete>
</DefaultRolloverStrategy>
With this configuration, logs older than 60 days will be auto-deleted.
Refer to the log4j2 docs for more configuration information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With