Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Log4j2 DefaultRolloverStrategy's max attribute really work?

I've configured a RollingRandomAccessFileAppender with only the OnStartupTriggeringPolicy set, but when I set the max attribute of the DefaultRolloverStrategy to some number, the logs keep generating past that amount indefinitely.

Here's my log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN">     <Appenders>         <Console name="Console" target="SYSTEM_OUT">             <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>         </Console>         <RollingRandomAccessFile              name="RollingRAF"              fileName="logs/app.log"             filePattern="logs/app-%d{[email protected]}.log">             <PatternLayout>                 <Pattern>%d %p %c{1.} %m%n</Pattern>             </PatternLayout>             <Policies>                 <OnStartupTriggeringPolicy />             </Policies>             <DefaultRolloverStrategy max="5"/>         </RollingRandomAccessFile>     </Appenders>     <Loggers>         <Logger name="myLogger" level="warn">             <AppenderRef ref="RollingRAF"/>         </Logger>         <Root level="error">             <AppenderRef ref="Console"/>         </Root>     </Loggers> </Configuration> 

Is it because I don't have an iterator in my name pattern?

Is it because my file name precision is set to seconds?

Is it because I only have the OnStartupTriggeringPolicy set?

Or what's going on here?

My goal here was to set up a rolling configuration that will log the last 5 application runs.

like image 316
Ceiling Gecko Avatar asked Jul 03 '14 10:07

Ceiling Gecko


People also ask

What is the default value of Max in DefaultRolloverStrategy in Log4j2 XML file?

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.

What is DefaultRolloverStrategy Max?

The DefaultRolloverStrategy is a combination of a time-based policy and a fixed-window policy. When the file name pattern contains a date format then the rollover time interval will be used to calculate the time to use in the file pattern.

What is Log4j2 rollover?

Log4j2 RollingFileAppender is an OutputStreamAppender that writes log messages to files, following a configured triggering policy about when a rollover (backup) should occur. It also has a configured rollover strategy about how to rollover the file.

How does log4j Appender work?

In the log4j2 architecture, an appender is basically responsible for sending log messages to a certain output destination. Here are some of the most useful types of appenders that the library provides: ConsoleAppender – logs messages to the System console. FileAppender – writes log messages to a file.


1 Answers

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.


Update:

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).

like image 154
Remko Popma Avatar answered Oct 10 '22 10:10

Remko Popma