How to remove old logs using log4j2

(FYI I have already searched a lot of documents on the Internet. I am using storm-0.10.0-beta1. The log4j2 configuration file in Storm is worker.xml)

Now I'm trying to use log4j2.

I am looking for a way to delete old logs, but I cannot figure it out. Part of the configuration is similar to below.

<RollingFile name="SERVICE_APPENDER" fileName="${sys:storm.home}/logs/${sys:logfile.name}.service" filePattern="${sys:storm.home}/logs/${sys:logfile.name}.service.%d{yyyyMMdd}"> <PatternLayout> <pattern>${pattern}</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> <DefaultRolloverStrategy max="9"/> </RollingFile> 

At first, I expected log files older than 3 days to be deleted.

But actually it is not.

So, I am wondering if there is a way to delete old logs or not.

If there is a way that I have not caught yet, let me know.

+5
source share
2 answers

Starting with version 2.5, Log4j supports the user action Delete , which is performed with each poll.

You can control which files are deleted using any combination:

  • Name ( glob or regex mapping)
  • Age ("remove if 14 days or older")
  • Count ("keep only the last 3")
  • Size ("save only the latest files up to 500 MB")

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

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

For your question, this snippet should work:

  <DefaultRolloverStrategy> <!-- * only files in the log folder, no sub folders * only rolled over log files (name match) * only files that are 4 days old or older --> <Delete basePath="${sys:storm.home}/logs/" maxDepth="1"> <IfFileName glob="*.service.????????" /> <IfLastModified age="4d" /> </Delete> </DefaultRolloverStrategy> 

Finally, be careful! Unable to recover files deleted this way. :-)

+4
source

You can find additional help information in this JIRA entry for log4j:

https://issues.apache.org/jira/browse/LOG4J2-524

It seems that automatically deleting old log files does not work if you only use TimeBasedTriggeringPolicy

+1
source

Source: https://habr.com/ru/post/1234121/


All Articles