Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j2 periodically cleaning log file

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)?

like image 300
SamCle88 Avatar asked Dec 23 '22 21:12

SamCle88


1 Answers

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.

like image 156
VHS Avatar answered Dec 26 '22 11:12

VHS