Reputation: 14154
Using Log4j2 v2.18.0 I want to roll over the log file every hour or every 100 MB, so I'm using this:
<Appenders>
<RollingFile name="APPLICATION" filePattern="/path/to/log.%d{yyyy-MM-dd.HH}.%i.log">
<Policies>
<TimeBasedTriggeringPolicy modulate="true"/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
</RollingFile>
</Appenders>
When using both policies together, my expectation was that the %i
integer counter would be relative to the time period (e.g., restart at 1 every hour), but instead it seems the integer counter keeps increasing. Is there a way to reset the counter or does it just increase forever until the JVM restarts?
What I expected:
/path/to/log.2023-03-22.11.1.log
/path/to/log.2023-03-22.12.1.log
/path/to/log.2023-03-22.12.2.log
What I got:
/path/to/log.2023-03-22.11.1.log
/path/to/log.2023-03-22.12.2.log
/path/to/log.2023-03-22.12.3.log
Based on https://logging.apache.org/log4j/2.x/manual/appenders.html#rollingfileappender this behavior appears to be a bug?
A RollingFileAppender requires a TriggeringPolicy and a RolloverStrategy. The triggering policy determines if a rollover should be performed while the RolloverStrategy defines how the rollover should be done. If no RolloverStrategy is configured, RollingFileAppender will use the DefaultRolloverStrategy. Since log4j-2.5, a custom delete action can be configured in the DefaultRolloverStrategy to run at rollover. Since 2.8 if no file name is configured then DirectWriteRolloverStrategy will be used instead of DefaultRolloverStrategy.
The DirectWriteRolloverStrategy causes log events to be written directly to files represented by the file pattern. With this strategy file renames are not performed. If the size-based triggering policy causes multiple files to be written during the specified time period they will be numbered starting at one and continually incremented until a time-based rollover occurs.
Upvotes: 2
Views: 299