From 999780f342c5719a3443a184638329c95282e2d9 Mon Sep 17 00:00:00 2001 From: Robert Thornton Date: Wed, 28 Nov 2018 17:31:53 -0700 Subject: [PATCH] Support for capping archived log files See gh-15325 --- .../appendix/application-properties.adoc | 3 + .../main/asciidoc/spring-boot-features.adoc | 15 ++++- .../boot/logging/LoggingSystemProperties.java | 14 +++++ .../logback/DefaultLogbackConfiguration.java | 6 ++ ...itional-spring-configuration-metadata.json | 14 +++++ .../boot/logging/logback/file-appender.xml | 2 + .../logback/LogbackLoggingSystemTests.java | 63 +++++++++++++++++++ 7 files changed, 116 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix/application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix/application-properties.adoc index 7776bdcf8d..2732f95322 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix/application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix/application-properties.adoc @@ -37,10 +37,13 @@ content into your application. Rather, pick only the properties that you need. # LOGGING logging.config= # Location of the logging configuration file. For instance, `classpath:logback.xml` for Logback. logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions. + logging.file= # Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory. logging.file.max-history=0 # Maximum of archive log files to keep. Only supported with the default logback setup. logging.file.max-size=10MB # Maximum log file size. Only supported with the default logback setup. logging.file.name= # Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory. logging.file.path= # Location of the log file. For instance, `/var/log`. + logging.file.total-size-cap=0 # Places a cap on the total size of log backups. Only supported with the default logback setup. + logging.file.clean-history-on-start=false # Whether to clean the archive log files on startup. Only supported with the default logback setup. logging.group.*= # Log groups to quickly change multiple loggers at the same time. For instance, `logging.level.db=org.hibernate,org.springframework.jdbc`. logging.level.*= # Log levels severity mapping. For instance, `logging.level.org.springframework=DEBUG`. logging.pattern.console= # Appender pattern for output to the console. Supported only with the default Logback setup. diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index e20d93b1f0..658c34666c 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -1738,7 +1738,10 @@ relative to the current directory. Log files rotate when they reach 10 MB and, as with console output, `ERROR`-level, `WARN`-level, and `INFO`-level messages are logged by default. Size limits can be changed using the `logging.file.max-size` property. Previously rotated files are archived -indefinitely unless the `logging.file.max-history` property has been set. +indefinitely unless the `logging.file.max-history` property has been set. The total size +of log archives can be capped using `logging.file.total-size-cap`. When the total size of +log archives exceeds that threshold, backups will be deleted. To force log archive cleanup +on application startup, use the `logging.file.clean-history-on-start` property. NOTE: The logging system is initialized early in the application lifecycle. Consequently, logging properties are not found in property files loaded through `@PropertySource` @@ -1870,6 +1873,16 @@ setup.) |Maximum number of archive log files to keep (if LOG_FILE enabled). (Only supported with the default Logback setup.) +|`logging.file.total-size-cap` +|`LOG_FILE_TOTAL_SIZE_CAP` +|The total size of log backups to be kept (if LOG_FILE enabled). (Only supported with +the default Logback setup.) + +|`logging.file.clean-history-on-start` +|`LOG_FILE_CLEAN_HISTORY_ON_START` +|Whether to clean the archive log files on startup (if LOG_FILE enabled). (Only supported with +the default Logback setup.) + |`logging.file.path` |`LOG_PATH` |If defined, it is used in the default log configuration. diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java index dca12f87c7..fb94a054d2 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java @@ -30,6 +30,7 @@ import org.springframework.util.Assert; * @author Phillip Webb * @author Madhura Bhave * @author Vedran Pavic + * @author Robert Thornton * @since 2.0.0 */ public class LoggingSystemProperties { @@ -74,6 +75,16 @@ public class LoggingSystemProperties { */ public static final String FILE_MAX_SIZE = "LOG_FILE_MAX_SIZE"; + /** + * The name of the System property that contains the file total size cap. + */ + public static final String FILE_TOTAL_SIZE_CAP = "LOG_FILE_TOTAL_SIZE_CAP"; + + /** + * The name of the System property that contains the clean history on start flag. + */ + public static final String FILE_CLEAN_HISTORY_ON_START = "LOG_FILE_CLEAN_HISTORY_ON_START"; + /** * The name of the System property that contains the log level pattern. */ @@ -108,6 +119,9 @@ public class LoggingSystemProperties { setSystemProperty(resolver, FILE_LOG_PATTERN, "pattern.file"); setSystemProperty(resolver, FILE_MAX_HISTORY, "file.max-history"); setSystemProperty(resolver, FILE_MAX_SIZE, "file.max-size"); + setSystemProperty(resolver, FILE_TOTAL_SIZE_CAP, "file.total-size-cap"); + setSystemProperty(resolver, FILE_CLEAN_HISTORY_ON_START, + "file.clean-history-on-start"); setSystemProperty(resolver, LOG_LEVEL_PATTERN, "pattern.level"); setSystemProperty(resolver, LOG_DATEFORMAT_PATTERN, "pattern.dateformat"); if (logFile != null) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java index c7af583b71..de3eb88f09 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java @@ -45,6 +45,7 @@ import org.springframework.util.ReflectionUtils; * @author Phillip Webb * @author Madhura Bhave * @author Vedran Pavic + * @author Robert Thornton * @since 1.1.2 */ class DefaultLogbackConfiguration { @@ -145,6 +146,11 @@ class DefaultLogbackConfiguration { this.patterns.getProperty("logging.file.max-size", MAX_FILE_SIZE)); rollingPolicy.setMaxHistory(this.patterns.getProperty("logging.file.max-history", Integer.class, CoreConstants.UNBOUND_HISTORY)); + rollingPolicy.setTotalSizeCap( + FileSize.valueOf(this.patterns.getProperty("logging.file.total-size-cap", + "" + CoreConstants.UNBOUNDED_TOTAL_SIZE_CAP))); + rollingPolicy.setCleanHistoryOnStart(Boolean.parseBoolean(this.patterns + .getProperty("logging.file.clean-history-on-start", "false"))); appender.setRollingPolicy(rollingPolicy); rollingPolicy.setParent(appender); config.start(rollingPolicy); diff --git a/spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json index dccde53dda..170002e4e9 100644 --- a/spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -102,6 +102,20 @@ "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener", "defaultValue": 0 }, + { + "name": "logging.file.total-size-cap", + "type": "java.lang.String", + "description": "Total size of log backups to be kept. Only supported with the default logback setup.", + "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener", + "defaultValue": 0 + }, + { + "name": "logging.file.clean-history-on-start", + "type": "java.lang.Boolean", + "description": "Whether to clean log backups on start. Only supported with the default logback setup.", + "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener", + "defaultValue": false + }, { "name": "logging.group", "type": "java.util.Map>", diff --git a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/file-appender.xml b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/file-appender.xml index deae1a96a6..25060f9a10 100644 --- a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/file-appender.xml +++ b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/file-appender.xml @@ -16,6 +16,8 @@ initialization performed by Boot ${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz ${LOG_FILE_MAX_SIZE:-10MB} ${LOG_FILE_MAX_HISTORY:-0} + ${LOG_FILE_TOTAL_SIZE_CAP:-0} + ${LOG_FILE_CLEAN_HISTORY_ON_START:-false} diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java index 3311653037..a2f9f1d84f 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java @@ -73,6 +73,7 @@ import static org.mockito.Mockito.verify; * @author Ben Hale * @author Madhura Bhave * @author Vedran Pavic + * @author Robert Thornton */ @RunWith(ModifiedClassPathRunner.class) @ClassPathExclusions("log4j-*.jar") @@ -411,6 +412,68 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { assertThat(getRollingPolicy().getMaxHistory()).isEqualTo(30); } + @Test + public void testTotalSizeCapProperty() throws Exception { + String expectedSize = "101 MB"; + MockEnvironment environment = new MockEnvironment(); + environment.setProperty("logging.file.total-size-cap", expectedSize); + LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext( + environment); + File file = new File(tmpDir(), "logback-test.log"); + LogFile logFile = getLogFile(file.getPath(), null); + this.loggingSystem.initialize(loggingInitializationContext, null, logFile); + this.logger.info("Hello world"); + assertThat(getLineWithText(file, "Hello world")).contains("INFO"); + assertThat(ReflectionTestUtils.getField(getRollingPolicy(), "totalSizeCap") + .toString()).isEqualTo(expectedSize); + } + + @Test + public void testTotalSizeCapPropertyWithXmlConfiguration() throws Exception { + String expectedSize = "101 MB"; + MockEnvironment environment = new MockEnvironment(); + environment.setProperty("logging.file.total-size-cap", expectedSize); + LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext( + environment); + File file = new File(tmpDir(), "logback-test.log"); + LogFile logFile = getLogFile(file.getPath(), null); + this.loggingSystem.initialize(loggingInitializationContext, + "classpath:logback-include-base.xml", logFile); + this.logger.info("Hello world"); + assertThat(getLineWithText(file, "Hello world")).contains("INFO"); + assertThat(ReflectionTestUtils.getField(getRollingPolicy(), "totalSizeCap") + .toString()).isEqualTo(expectedSize); + } + + @Test + public void testCleanHistoryOnStartProperty() throws Exception { + MockEnvironment environment = new MockEnvironment(); + environment.setProperty("logging.file.clean-history-on-start", "true"); + LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext( + environment); + File file = new File(tmpDir(), "logback-test.log"); + LogFile logFile = getLogFile(file.getPath(), null); + this.loggingSystem.initialize(loggingInitializationContext, null, logFile); + this.logger.info("Hello world"); + assertThat(getLineWithText(file, "Hello world")).contains("INFO"); + assertThat(getRollingPolicy().isCleanHistoryOnStart()).isTrue(); + } + + @Test + public void testCleanHistoryOnStartPropertyWithXmlConfiguration() throws Exception { + MockEnvironment environment = new MockEnvironment(); + environment.setProperty("logging.file.clean-history-on-start", "true"); + LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext( + environment); + File file = new File(tmpDir(), "logback-test.log"); + LogFile logFile = getLogFile(file.getPath(), null); + this.loggingSystem.initialize(loggingInitializationContext, + "classpath:logback-include-base.xml", logFile); + this.logger.info("Hello world"); + assertThat(getLineWithText(file, "Hello world")).contains("INFO"); + assertThat(getRollingPolicy().isCleanHistoryOnStart()).isTrue(); + } + @Test public void exceptionsIncludeClassPackaging() { this.loggingSystem.beforeInitialize();