Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete older rolled over log4j2 logs, keeping up to 10 files?

What I want is:

  • Maximum of 10 log files, in total
  • Each log file, not more than 50MB in size.

Thus the logs folder never grows over (50MB *10 )= 500MB.

But it seems my log4j2 config is not properly done.

What is happening is:

  • Logs do roll over after 50 MB
  • But there are upto 10 logs kept per day
  • Thus there is no limit of number of log files kept in log folder (since for eg, in 2 days, 20 logs of 50mb each have collected)

Here is the config:

<Configuration status="WARN">
    <Appenders>
        <RollingFile name="RollingFile" fileName="log/my.log" filePattern="log/my-%d{MM-dd-yyyy}-%i.log">
          <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
          </PatternLayout>
          <Policies>
                <OnStartupTriggeringPolicy />
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="50 MB"/>
          </Policies>
          <DefaultRolloverStrategy max="10"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="RollingFile"/>
        </Root>
    </Loggers>
</Configuration>

What am I doing wrong?

like image 231
pdeva Avatar asked Jan 09 '16 10:01

pdeva


People also ask

Does log4j delete old logs?

Note that log4j2 does have a method for deleting old log files via the DefaultRolloverStrategy – but it is not in the sample log4j2. xml file that is distributed. The 50 days comes from age=”50d” which can be changed.

What is rollover strategy in log4j2?

Default Rollover Strategy. The default rollover strategy accepts both a date/time pattern and an integer from the filePattern attribute specified on the RollingFileAppender itself. If the date/time pattern is present it will be replaced with the current date and time values.

Can we delete log4j?

The upgrade-backup folder contains multiple log4j files under /update-backup/<release>/dsdata/elasticsearch_#. #. #/lib. It is safe for those to be deleted.

Where should I set log4j2 formatMsgNoLookups to true?

You can do this: %m{nolookups} in the layout. {nolookups} is how you set the property log4j2. formatMsgNoLookups=true within the configuration XML content.


2 Answers

Since 2.5, Log4j supports a custom Delete action that is executed on every rollover.

You can control which files are deleted by:

  1. Name (matching a glob or a regex)
  2. Age ("delete if 14 days old or older")
  3. Count ("keep only the most recent 3")
  4. Size ("keep only the most recent files up to 500MB")

The above can be combined. Instead of only specifying a size condition to keep disk usage down to max 500MB, it's a good idea to also match the name so you don't inadvertently delete unrelated files.

Users who need even more fine-grained control over which files to delete can specify a script condition using any supported JSR-223 scripting language.

Please check out the documentation, it has three full examples that may be useful.

For your question, this snippet may work:

  <DefaultRolloverStrategy>
    <!--
      * only files in the log folder, no sub folders
      * only rolled over log files (name match)
      * either when more than 10 matching files exist or when the max disk usage is exceeded
    -->
    <Delete basePath="log" maxDepth="1">
      <IfFileName glob="my-??-??-????-*.log">
        <IfAny>
          <IfAccumulatedFileSize exceeds="500 MB" />
          <IfAccumulatedFileCount exceeds="10" />
        </IfAny>
      </IfFileName>
    </Delete>
  </DefaultRolloverStrategy>

As an aside, note that you can compress log files on rollover to make them take up less disk space.

Finally, be careful! There is no way to recover files deleted this way. :-)

like image 115
Remko Popma Avatar answered Oct 29 '22 00:10

Remko Popma


The TimeBasedTriggeringPolicy works based of the filePattern. Basically, the smallest unit of time in the file pattern (%d) is the triggering time interval. In your case the value is 'dd' hence the policy is triggered every day.

The presence of %i in your filePattern keeps multiple log files for a day. I would recommend trying without the %i in the filePattern.

like image 25
Taha Avatar answered Oct 29 '22 00:10

Taha