diff --git a/core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4j2LoggingSystemProperties.java b/core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4j2LoggingSystemProperties.java index 4be71e3f622c..980b2ca774c8 100644 --- a/core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4j2LoggingSystemProperties.java +++ b/core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4j2LoggingSystemProperties.java @@ -63,7 +63,9 @@ protected void apply(@Nullable LogFile logFile, PropertyResolver resolver) { private void applyRollingPolicyProperties(PropertyResolver resolver) { applyRollingPolicy(RollingPolicySystemProperty.FILE_NAME_PATTERN, resolver); + applyRollingPolicy(RollingPolicySystemProperty.CLEAN_HISTORY_ON_START, resolver); applyRollingPolicy(RollingPolicySystemProperty.MAX_FILE_SIZE, resolver, DataSize.class); + applyRollingPolicy(RollingPolicySystemProperty.TOTAL_SIZE_CAP, resolver, DataSize.class); applyRollingPolicy(RollingPolicySystemProperty.MAX_HISTORY, resolver); applyRollingPolicy(RollingPolicySystemProperty.STRATEGY, resolver); applyRollingPolicy(RollingPolicySystemProperty.TIME_INTERVAL, resolver, Integer.class); diff --git a/core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/RollingPolicySystemProperty.java b/core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/RollingPolicySystemProperty.java index f5524cf4d3ca..6f890dc8a7ae 100644 --- a/core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/RollingPolicySystemProperty.java +++ b/core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/RollingPolicySystemProperty.java @@ -61,7 +61,17 @@ public enum RollingPolicySystemProperty { /** * Logging system property for the cron based schedule. */ - CRON("cron"); + CRON("cron"), + + /** + * Logging system property for the clean history on start flag. + */ + CLEAN_HISTORY_ON_START("clean-history-on-start"), + + /** + * Logging system property for the file total size cap. + */ + TOTAL_SIZE_CAP("total-size-cap"); private final String environmentVariableName; diff --git a/core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java b/core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java index c9c64e60f597..be0f7134698e 100644 --- a/core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java +++ b/core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java @@ -817,6 +817,8 @@ void rollingPolicySystemPropertiesAreApplied() { this.environment.setProperty("logging.log4j2.rollingpolicy.max-history", "30"); this.environment.setProperty("logging.log4j2.rollingpolicy.file-name-pattern", "${LOG_FILE}.%d{yyyy-MM-dd}.%i.log"); + this.environment.setProperty("logging.log4j2.rollingpolicy.clean-history-on-start", "true"); + this.environment.setProperty("logging.log4j2.rollingpolicy.total-size-cap", "1GB"); File file = new File(tmpDir(), "log4j2-test.log"); LogFile logFile = getLogFile(file.getPath(), null); this.loggingSystem.getSystemProperties(this.environment).apply(logFile); @@ -825,9 +827,14 @@ void rollingPolicySystemPropertiesAreApplied() { String maxFileSize = System.getProperty("LOG4J2_ROLLINGPOLICY_MAX_FILE_SIZE"); String maxHistory = System.getProperty("LOG4J2_ROLLINGPOLICY_MAX_HISTORY"); String fileNamePattern = System.getProperty("LOG4J2_ROLLINGPOLICY_FILE_NAME_PATTERN"); + String cleanHistoryOnStart = System.getProperty("LOG4J2_ROLLINGPOLICY_CLEAN_HISTORY_ON_START"); + String totalSizeCap = System.getProperty("LOG4J2_ROLLINGPOLICY_TOTAL_SIZE_CAP"); assertThat(maxFileSize).isEqualTo(String.valueOf(50 * 1024 * 1024)); assertThat(maxHistory).isEqualTo("30"); assertThat(fileNamePattern).isEqualTo("${LOG_FILE}.%d{yyyy-MM-dd}.%i.log"); + assertThat(cleanHistoryOnStart).isEqualTo("true"); + assertThat(totalSizeCap).isEqualTo(String.valueOf(DataSize.ofGigabytes(1).toBytes())); + } @Test diff --git a/core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4j2LoggingSystemPropertiesTests.java b/core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4j2LoggingSystemPropertiesTests.java index a15703d5b5fa..ec8b80625d2a 100644 --- a/core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4j2LoggingSystemPropertiesTests.java +++ b/core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4j2LoggingSystemPropertiesTests.java @@ -68,6 +68,8 @@ void appliesLog4j2RollingPolicyProperties() { this.environment.setProperty("logging.log4j2.rollingpolicy.time-interval", "2"); this.environment.setProperty("logging.log4j2.rollingpolicy.time-modulate", "true"); this.environment.setProperty("logging.log4j2.rollingpolicy.cron", "0 0 1 * * ?"); + this.environment.setProperty("logging.log4j2.rollingpolicy.clean-history-on-start", "true"); + this.environment.setProperty("logging.log4j2.rollingpolicy.total-size-cap", "1GB"); new Log4j2LoggingSystemProperties(this.environment).apply(null); @@ -78,7 +80,9 @@ void appliesLog4j2RollingPolicyProperties() { .containsEntry("LOG4J2_ROLLINGPOLICY_STRATEGY", "time") .containsEntry("LOG4J2_ROLLINGPOLICY_TIME_INTERVAL", "2") .containsEntry("LOG4J2_ROLLINGPOLICY_TIME_MODULATE", "true") - .containsEntry("LOG4J2_ROLLINGPOLICY_CRON", "0 0 1 * * ?"); + .containsEntry("LOG4J2_ROLLINGPOLICY_CRON", "0 0 1 * * ?") + .containsEntry("LOG4J2_ROLLINGPOLICY_CLEAN_HISTORY_ON_START", "true") + .containsEntry("LOG4J2_ROLLINGPOLICY_TOTAL_SIZE_CAP", String.valueOf(DataSize.ofGigabytes(1).toBytes())); } @Test