Skip to content

Conversation

hojooo
Copy link

@hojooo hojooo commented Sep 17, 2025

Summary

Hello! Log4j2 does not support rolling policies. So this PR adds basic rolling policy configuration property support for Log4j2. It introduces Log4j2 specific properties equivalent to the existing logging.logback.rollingpolicy.* properties, enabling consistent logging configuration through application.properties across different logging implementations. 
And advanced rolling strategy support to the Log4j2 rolling policy configuration. It introduces the SpringBootTriggeringPolicy plugin to enable various rolling strategies and strategy-specific detailed configuration through application.properties.

Key Changes

1. Standardized Log4j2 Rolling Policy Properties (b98d74f)

We've added standard properties for Log4j2, similar to the existing logging.logback.rollingpolicy.* properties. Users can now easily control the rolling policy using the following attributes in application.properties:

# Basic rolling policy configuration (file size, history, etc.)
logging.log4j2.rollingpolicy.max-file-size=10MB
logging.log4j2.rollingpolicy.max-history=7
logging.log4j2.rollingpolicy.total-size-cap=1GB
logging.log4j2.rollingpolicy.clean-history-on-start=false

# Rolled file name pattern configuration
logging.log4j2.rollingpolicy.file-name-pattern=${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz

2. Advanced Rolling Strategy (for commits after ddda65e)

A custom Log4j2 plugin, SpringBootTriggeringPolicy, has been introduced to support various advanced rolling strategies beyond simple size-based rolling.

  • size (default): Rolls files based on their size.
  • time: Rolls files based on a time interval.
  • size-and-time: Rolls when both size and time conditions are met.
  • cron: Rolls based on a cron expression schedule.

Implementation Details

1. Property Standardization Implementation

  • Log4j2RollingPolicySystemProperty Enum: An enum was implemented to map Spring Environment properties to system properties that Log4j2 can recognize (e.g., max-file-sizeLOG4J2_ROLLINGPOLICY_MAX_FILE_SIZE).
  • Log4j2LoggingSystemProperties Class: This class handles the automatic conversion of Spring's DataSize type to bytes and ensures backward compatibility with deprecated properties like logging.file.max-size.
  • log4j2-file.xml Modification: The configuration file was updated to reference the newly defined system properties using the ${sys:PROPERTY_NAME} syntax.

2. Advanced Rolling Strategy Implementation

  • SpringBootTriggeringPolicy Plugin: A custom TriggeringPolicy was implemented using Log4j2's @Plugin annotation. Based on the strategy property, this plugin internally selects and delegates to the appropriate policy (e.g., SizeBasedTriggeringPolicy, TimeBasedTriggeringPolicy).
  • log4j2-file.xml Update: The <Policies> block was modified to use SpringBootTriggeringPolicy, with its parameters (maxFileSize, timeInterval, cronExpression, etc.) configured via system properties.

Configuration Examples

Time-based Rolling

logging.log4j2.rollingpolicy.strategy=time
logging.log4j2.rollingpolicy.time-based.interval=1
logging.log4j2.rollingpolicy.time-based.modulate=true
logging.log4j2.rollingpolicy.file-name-pattern=${LOG_FILE}.%d{yyyy-MM-dd}.gz

Size and Time Composite Rolling

logging.log4j2.rollingpolicy.strategy=size-and-time
logging.log4j2.rollingpolicy.max-file-size=50MB
logging.log4j2.rollingpolicy.time-based.interval=6
logging.log4j2.rollingpolicy.file-name-pattern=${LOG_FILE}.%d{yyyy-MM-dd-HH}.%i.gz

Cron Expression-based Rolling

logging.log4j2.rollingpolicy.strategy=cron
logging.log4j2.rollingpolicy.cron.schedule=0 0 5 ? * MON
logging.log4j2.rollingpolicy.file-name-pattern=${LOG_FILE}.%d{yyyy-MM-dd}.gz

Compatibility

  • backward compatibility is guaranteed.
  • Existing properties like logging.file.max-size and logging.file.max-history continue to work as expected.
  • If new logging.log4j2.rollingpolicy.* properties are set, they will take precedence over the older properties.
  • If no relevant properties are configured, the existing default behavior (10MB max-file-size, 7-day max-history) is maintained.

hojooo and others added 5 commits September 18, 2025 00:19
      - Add SpringBootTriggeringPolicy plugin supporting size, time, size-and-time, and cron strategies
      - Read rolling strategy parameters from LOG4J2_ROLLINGPOLICY_* system properties (fallback to attributes)
      - Register Boot plugin package in Log4J2LoggingSystem to ensure plugin discovery
      - Use SpringBootTriggeringPolicy under a top-level Policies wrapper in log4j2-file.xml

Signed-off-by: hojooo <[email protected]>
      - Introduce rolling policy properties: strategy, time-based.interval, time-based.modulate, cron.schedule
      - Extend Log4j2RollingPolicySystemProperty with STRATEGY, TIME_INTERVAL, TIME_MODULATE, CRON_SCHEDULE
      - Propagate new properties in Log4j2LoggingSystemProperties (with deprecated fallback guarded for null)
      - Document new properties in configuration metadata
      - Update Log4j2LoggingSystemPropertiesTests to assert new system properties mappingAdd Log4j2 rolling policy configuration support

Signed-off-by: hojooo <[email protected]>
      - Initialize with classpath:org/springframework/boot/logging/log4j2/log4j2-file.xml to validate file-based rolling
      - Unwrap CompositeTriggeringPolicy to locate nested SpringBootTriggeringPolicy and assert its delegate
      - Keep assertions for time, size-and-time, and cron strategies

Signed-off-by: hojooo <[email protected]>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 17, 2025
@philwebb philwebb added the for: team-meeting An issue we'd like to discuss as a team to make progress label Sep 17, 2025
@hojooo hojooo force-pushed the log4j2-rolling-policy branch from 66fcaf7 to b5d3b84 Compare September 18, 2025 03:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: team-meeting An issue we'd like to discuss as a team to make progress status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants