From 999780f342c5719a3443a184638329c95282e2d9 Mon Sep 17 00:00:00 2001 From: Robert Thornton Date: Wed, 28 Nov 2018 17:31:53 -0700 Subject: [PATCH 1/2] 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(); From f42cec9eaca6fdd19baeed6e6f0380528962d638 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 12 Feb 2019 16:29:22 +0100 Subject: [PATCH 2/2] Polish "Support for capping archived log files" Closes gh-15325 --- .../appendix/application-properties.adoc | 5 +- .../main/asciidoc/spring-boot-features.adoc | 20 +++--- .../boot/logging/LoggingSystemProperties.java | 16 ++--- .../logback/DefaultLogbackConfiguration.java | 8 +-- ...itional-spring-configuration-metadata.json | 16 ++--- .../boot/logging/logback/file-appender.xml | 2 +- .../logback/LogbackLoggingSystemTests.java | 62 +++++++++---------- 7 files changed, 64 insertions(+), 65 deletions(-) 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 2732f95322..29fea794fb 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,13 +37,12 @@ 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.clean-history-on-start=false # Whether to clean the archive log files on startup. Only supported with the default logback setup. 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.file.total-size-cap=0 # Total size of log backups to be kept. 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 658c34666c..bc72fe79d8 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 @@ -1859,6 +1859,11 @@ To help with the customization, some other properties are transferred from the S |`LOG_EXCEPTION_CONVERSION_WORD` |The conversion word used when logging exceptions. +|`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.name` |`LOG_FILE` |If defined, it is used in the default log configuration. @@ -1873,20 +1878,15 @@ 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. +|`logging.file.total-size-cap` +|`LOG_FILE_TOTAL_SIZE_CAP` +|Total size of log backups to be kept (if LOG_FILE enabled). (Only supported with the +default Logback setup.) + |`logging.pattern.console` |`CONSOLE_LOG_PATTERN` |The log pattern to use on the console (stdout). (Only supported with the default Logback 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 fb94a054d2..66513a9a86 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -60,6 +60,11 @@ public class LoggingSystemProperties { */ public static final String CONSOLE_LOG_PATTERN = "CONSOLE_LOG_PATTERN"; + /** + * 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 file log pattern. */ @@ -80,11 +85,6 @@ public class LoggingSystemProperties { */ 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. */ @@ -117,11 +117,11 @@ public class LoggingSystemProperties { setSystemProperty(PID_KEY, new ApplicationPid().toString()); setSystemProperty(resolver, CONSOLE_LOG_PATTERN, "pattern.console"); setSystemProperty(resolver, FILE_LOG_PATTERN, "pattern.file"); + setSystemProperty(resolver, FILE_CLEAN_HISTORY_ON_START, + "file.clean-history-on-start"); 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 de3eb88f09..bf1efdc005 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -141,6 +141,8 @@ class DefaultLogbackConfiguration { private void setRollingPolicy(RollingFileAppender appender, LogbackConfigurator config, String logFile) { SizeAndTimeBasedRollingPolicy rollingPolicy = new SizeAndTimeBasedRollingPolicy<>(); + rollingPolicy.setCleanHistoryOnStart(this.patterns.getProperty( + "logging.file.clean-history-on-start", Boolean.class, false)); rollingPolicy.setFileNamePattern(logFile + ".%d{yyyy-MM-dd}.%i.gz"); setMaxFileSize(rollingPolicy, this.patterns.getProperty("logging.file.max-size", MAX_FILE_SIZE)); @@ -148,9 +150,7 @@ class DefaultLogbackConfiguration { 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"))); + String.valueOf(CoreConstants.UNBOUNDED_TOTAL_SIZE_CAP)))); 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 170002e4e9..0642fd831d 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 @@ -76,6 +76,13 @@ "replacement": "logging.file.name" } }, + { + "name": "logging.file.clean-history-on-start", + "type": "java.lang.Boolean", + "description": "Whether to clean the archive log files on startup. Only supported with the default logback setup.", + "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener", + "defaultValue": false + }, { "name": "logging.file.name", "type": "java.lang.String", @@ -107,14 +114,7 @@ "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 + "defaultValue": "0" }, { "name": "logging.group", 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 25060f9a10..f081a5ade5 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 @@ -13,11 +13,11 @@ initialization performed by Boot ${LOG_FILE} + ${LOG_FILE_CLEAN_HISTORY_ON_START:-false} ${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 a2f9f1d84f..617e1a30c7 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 @@ -352,6 +352,35 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { assertThat(getLineWithText(file, "Hello world")).doesNotContain("INFO"); } + @Test + public void testCleanHistoryOnStartProperty() { + 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() { + 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 testMaxFileSizeProperty() { MockEnvironment environment = new MockEnvironment(); @@ -413,7 +442,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { } @Test - public void testTotalSizeCapProperty() throws Exception { + public void testTotalSizeCapProperty() { String expectedSize = "101 MB"; MockEnvironment environment = new MockEnvironment(); environment.setProperty("logging.file.total-size-cap", expectedSize); @@ -429,7 +458,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { } @Test - public void testTotalSizeCapPropertyWithXmlConfiguration() throws Exception { + public void testTotalSizeCapPropertyWithXmlConfiguration() { String expectedSize = "101 MB"; MockEnvironment environment = new MockEnvironment(); environment.setProperty("logging.file.total-size-cap", expectedSize); @@ -445,35 +474,6 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { .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();