Skip to content

Commit c986c3c

Browse files
committed
Log4j2: Add property-driven rolling strategy support and metadata
- 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]>
1 parent 5917b1c commit c986c3c

File tree

4 files changed

+71
-11
lines changed

4 files changed

+71
-11
lines changed

core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4j2LoggingSystemProperties.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ protected void apply(@Nullable LogFile logFile, PropertyResolver resolver) {
7171
}
7272

7373
private void applyRollingPolicyProperties(PropertyResolver resolver) {
74+
applyRollingPolicy(Log4j2RollingPolicySystemProperty.STRATEGY, resolver);
75+
applyRollingPolicy(Log4j2RollingPolicySystemProperty.TIME_INTERVAL, resolver, Integer.class);
76+
applyRollingPolicy(Log4j2RollingPolicySystemProperty.TIME_MODULATE, resolver, Boolean.class);
77+
applyRollingPolicy(Log4j2RollingPolicySystemProperty.CRON_SCHEDULE, resolver);
7478
applyRollingPolicy(Log4j2RollingPolicySystemProperty.FILE_NAME_PATTERN, resolver);
7579
applyRollingPolicy(Log4j2RollingPolicySystemProperty.CLEAN_HISTORY_ON_START, resolver);
7680
applyRollingPolicy(Log4j2RollingPolicySystemProperty.MAX_FILE_SIZE, resolver, DataSize.class);
@@ -85,7 +89,9 @@ private void applyRollingPolicy(Log4j2RollingPolicySystemProperty property, Prop
8589
private <T> void applyRollingPolicy(Log4j2RollingPolicySystemProperty property, PropertyResolver resolver,
8690
Class<T> type) {
8791
T value = getProperty(resolver, property.getApplicationPropertyName(), type);
88-
value = (value != null) ? value : getProperty(resolver, property.getDeprecatedApplicationPropertyName(), type);
92+
if (value == null && property.getDeprecatedApplicationPropertyName() != null) {
93+
value = getProperty(resolver, property.getDeprecatedApplicationPropertyName(), type);
94+
}
8995
if (value != null) {
9096
String stringValue = String.valueOf((value instanceof DataSize dataSize) ? dataSize.toBytes() : value);
9197
setSystemProperty(property.getEnvironmentVariableName(), stringValue);

core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4j2RollingPolicySystemProperty.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.logging.log4j2;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
/**
2022
* Log4j2 rolling policy system properties that can later be used by log configuration
2123
* files.
@@ -48,15 +50,35 @@ public enum Log4j2RollingPolicySystemProperty {
4850
/**
4951
* Logging system property for the file log max history.
5052
*/
51-
MAX_HISTORY("max-history", "logging.file.max-history");
53+
MAX_HISTORY("max-history", "logging.file.max-history"),
54+
55+
/**
56+
* Logging system property for the rolling policy strategy.
57+
*/
58+
STRATEGY("strategy", null),
59+
60+
/**
61+
* Logging system property for the rolling policy time interval.
62+
*/
63+
TIME_INTERVAL("time-based.interval", null),
64+
65+
/**
66+
* Logging system property for the rolling policy time modulate flag.
67+
*/
68+
TIME_MODULATE("time-based.modulate", null),
69+
70+
/**
71+
* Logging system property for the cron based schedule.
72+
*/
73+
CRON_SCHEDULE("cron.schedule", null);
5274

5375
private final String environmentVariableName;
5476

5577
private final String applicationPropertyName;
5678

57-
private final String deprecatedApplicationPropertyName;
79+
private final @Nullable String deprecatedApplicationPropertyName;
5880

59-
Log4j2RollingPolicySystemProperty(String applicationPropertyName, String deprecatedApplicationPropertyName) {
81+
Log4j2RollingPolicySystemProperty(String applicationPropertyName, @Nullable String deprecatedApplicationPropertyName) {
6082
this.environmentVariableName = "LOG4J2_ROLLINGPOLICY_" + name();
6183
this.applicationPropertyName = "logging.log4j2.rollingpolicy." + applicationPropertyName;
6284
this.deprecatedApplicationPropertyName = deprecatedApplicationPropertyName;
@@ -74,7 +96,7 @@ String getApplicationPropertyName() {
7496
return this.applicationPropertyName;
7597
}
7698

77-
String getDeprecatedApplicationPropertyName() {
99+
@Nullable String getDeprecatedApplicationPropertyName() {
78100
return this.deprecatedApplicationPropertyName;
79101
}
80102

core/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,33 @@
141141
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
142142
"defaultValue": false
143143
},
144+
{
145+
"name": "logging.log4j2.rollingpolicy.strategy",
146+
"type": "java.lang.String",
147+
"description": "Rolling policy strategy. Supported values are 'size', 'time', 'size-and-time', and 'cron'.",
148+
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
149+
"defaultValue": "size"
150+
},
151+
{
152+
"name": "logging.log4j2.rollingpolicy.time-based.interval",
153+
"type": "java.lang.Integer",
154+
"description": "Time based triggering interval when the strategy is 'time' or 'size-and-time'.",
155+
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
156+
"defaultValue": 1
157+
},
158+
{
159+
"name": "logging.log4j2.rollingpolicy.time-based.modulate",
160+
"type": "java.lang.Boolean",
161+
"description": "Whether to align the next rollover time to occur at the top of the interval when the strategy is time based.",
162+
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
163+
"defaultValue": false
164+
},
165+
{
166+
"name": "logging.log4j2.rollingpolicy.cron.schedule",
167+
"type": "java.lang.String",
168+
"description": "Cron expression used when the strategy is 'cron'.",
169+
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener"
170+
},
144171
{
145172
"name": "logging.log4j2.rollingpolicy.file-name-pattern",
146173
"type": "java.lang.String",

core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4j2LoggingSystemPropertiesTests.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ void appliesLog4j2RollingPolicyProperties() {
5252
Environment environment = new StandardEnvironment();
5353
addPropertiesToEnvironment(environment, "logging.log4j2.rollingpolicy.max-file-size", "50MB",
5454
"logging.log4j2.rollingpolicy.clean-history-on-start", "true",
55-
"logging.log4j2.rollingpolicy.max-history", "30",
56-
"logging.log4j2.rollingpolicy.total-size-cap", "10GB",
57-
"logging.log4j2.rollingpolicy.file-name-pattern", "test.%d{yyyy-MM-dd}.%i.log");
55+
"logging.log4j2.rollingpolicy.max-history", "30", "logging.log4j2.rollingpolicy.total-size-cap", "10GB",
56+
"logging.log4j2.rollingpolicy.file-name-pattern", "test.%d{yyyy-MM-dd}.%i.log",
57+
"logging.log4j2.rollingpolicy.strategy", "time", "logging.log4j2.rollingpolicy.time-based.interval",
58+
"2", "logging.log4j2.rollingpolicy.time-based.modulate", "true",
59+
"logging.log4j2.rollingpolicy.cron.schedule", "0 0 0 * * ?");
5860
new Log4j2LoggingSystemProperties(environment, this.systemPropertySetter).apply(null);
5961
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_MAX_FILE_SIZE",
6062
String.valueOf(DataSize.ofMegabytes(50).toBytes()));
@@ -64,6 +66,10 @@ void appliesLog4j2RollingPolicyProperties() {
6466
String.valueOf(DataSize.ofGigabytes(10).toBytes()));
6567
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_FILE_NAME_PATTERN",
6668
"test.%d{yyyy-MM-dd}.%i.log");
69+
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_STRATEGY", "time");
70+
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_TIME_INTERVAL", "2");
71+
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_TIME_MODULATE", "true");
72+
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_CRON_SCHEDULE", "0 0 0 * * ?");
6773
}
6874

6975
@Test
@@ -111,8 +117,7 @@ private void addPropertiesToEnvironment(Environment environment, String... pairs
111117
for (int i = 0; i < pairs.length; i += 2) {
112118
map.put(pairs[i], pairs[i + 1]);
113119
}
114-
((StandardEnvironment) environment).getPropertySources()
115-
.addFirst(new MapPropertySource("test", map));
120+
((StandardEnvironment) environment).getPropertySources().addFirst(new MapPropertySource("test", map));
116121
}
117122

118-
}
123+
}

0 commit comments

Comments
 (0)