Starting with version 2.5, Log4j supports a custom delete action , which is performed with each poll.
You can control which files are deleted:
- 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")
The above may be combined. Instead of specifying only a size condition to maintain disk space up to a maximum of 500 MB, it is recommended that you also map the name so that you do not accidentally delete unrelated files.
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 may work:
<DefaultRolloverStrategy> <Delete basePath="log" maxDepth="1"> <IfFileName glob="my-??-??-????-*.log"> <IfAny> <IfAccumulatedFileSize exceeds="500 MB" /> <IfAccumulatedFileCount exceeds="10" /> </IfAny> </IfFileName> </Delete> </DefaultRolloverStrategy>
As a note, note that compress log files when capsizing so they take up less disk space.
Finally, be careful! Unable to recover files deleted this way. :-)
source share