What I want is:
Thus the logs folder never grows over (50MB *10 )= 500MB.
But it seems my log4j2 config is not properly done.
What is happening is:
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?
Since 2.5, Log4j supports a custom Delete action that is executed on every rollover.
You can control which files are deleted by:
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. :-)