How does Log4j2 DefaultRolloverStrategy’s max attribute really work?

The DefaultRolloverStrategy will use the date pattern specified in the filePattern if a TimeBasedTriggeringPolicy is specified. To use the max attribute, specify a %i pattern in the filePattern, and add <SizeBasedTriggeringPolicy size="20 MB" /> to the rollover policies. (Or some other size of course.)

The value for max in <DefaultRolloverStrategy max="5"/> will then ensure that within the same rollover period (one second for you since you specified a date pattern of %d{[email protected]}) no more than 5 files will be created when a size-based rollover was triggered.

This is more useful if your rollover window is longer, like rolling over to a new folder every day, and within that folder, ensure that no more than 5 files are created with max size=20 MB.


Log4j 2.5 added the ability to configure custom delete actions. Out of the box you can delete files based on age, count or how much disk space they take up (accumulated file size).

Leave a Comment