Size-Based Log Rotation Policy¶
A Size-Based Log Rotation Policy is a configuration strategy used in logging frameworks (such as Log4j 2) to automatically manage log file growth by triggering a rollover based on file size limits.
Configuration¶
In the context of Log4j 2 XML configuration, this policy is implemented using the SizeBasedTriggeringPolicy element within a RollingFile appender's Policies section.^[600-developer-java-3-party-java-log4j2.md] The policy requires a specific size attribute (e.g., 19500KB) which defines the threshold at which the current active log file is closed and a new one is created.^[600-developer-java-3-party-java-log4j2.md]
Components¶
- Triggering Policy: The
SizeBasedTriggeringPolicymonitors the file size and initiates the rollover process when the specified size is reached.^[600-developer-java-3-party-java-log4j2.md] - File Pattern: The
filePatternattribute defines the naming convention for the archived files (e.g.,main-%d{yyyy-MM-dd}-%i.log), which typically includes date and index variables to differentiate rotated logs.^[600-developer-java-3-party-java-log4j2.md] - Rollover Strategy: A
DefaultRolloverStrategyis often used in conjunction with the size-based policy to specify the maximum number of archived files to retain (e.g.,max="10").^[600-developer-java-3-party-java-log4j2.md]
Related Concepts¶
- Log4j 2
- [[Logging]]
- [[Archived logs]]
Sources¶
^[600-developer-java-3-party-java-log4j2.md]