From f0327fbd0d483411d62a33082c41d28cbefa2819 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 2 Nov 2017 15:17:46 -0700 Subject: [PATCH] Polish SizeAndTimeBasedRollingPolicy changes Closes gh-6352 --- .../main/asciidoc/spring-boot-features.adoc | 7 +++--- .../logback/DefaultLogbackConfiguration.java | 23 +++++++++++-------- .../logback/LogbackLoggingSystemTests.java | 12 +++++----- 3 files changed, 23 insertions(+), 19 deletions(-) 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 ef02e94124..71eff5ac2d 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 @@ -1624,11 +1624,13 @@ To help with the customization, some other properties are transferred from the S |`logging.file.max-size` |`LOG_FILE_MAX_SIZE` -|Maximum log file size (if LOG_FILE enabled). (Only supported with the default logback setup.) +|Maximum log file size (if LOG_FILE enabled). (Only supported with the default logback + setup.) |`logging.file.max-history` |`LOG_FILE_MAX_HISTORY` -|Maximum number of archive log files to keep (if LOG_FILE enabled). (Only supported with the default logback setup.) +|Maximum number of archive log files to keep (if LOG_FILE enabled). (Only supported with + the default logback setup.) |`logging.path` |`LOG_PATH` @@ -1655,7 +1657,6 @@ To help with the customization, some other properties are transferred from the S environment variable). |=== - All the supported logging systems can consult System properties when parsing their configuration files. See the default configurations in `spring-boot.jar` for examples: 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 c41d69b986..42490fa333 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 @@ -149,11 +149,20 @@ class DefaultLogbackConfiguration { private void setRollingPolicy(RollingFileAppender appender, LogbackConfigurator config, String logFile) { - SizeAndTimeBasedRollingPolicy rollingPolicy = - new SizeAndTimeBasedRollingPolicy<>(); + SizeAndTimeBasedRollingPolicy rollingPolicy = new SizeAndTimeBasedRollingPolicy<>(); rollingPolicy.setFileNamePattern(logFile + ".%d{yyyy-MM-dd}.%i.gz"); - String maxFileSize = this.patterns.getProperty("logging.file.max-size", - MAX_FILE_SIZE); + setMaxFileSize(rollingPolicy, + this.patterns.getProperty("logging.file.max-size", MAX_FILE_SIZE)); + rollingPolicy.setMaxHistory(this.patterns.getProperty("logging.file.max-history", + Integer.class, CoreConstants.UNBOUND_HISTORY)); + appender.setRollingPolicy(rollingPolicy); + rollingPolicy.setParent(appender); + config.start(rollingPolicy); + } + + private void setMaxFileSize( + SizeAndTimeBasedRollingPolicy rollingPolicy, + String maxFileSize) { try { rollingPolicy.setMaxFileSize(FileSize.valueOf(maxFileSize)); } @@ -163,12 +172,6 @@ class DefaultLogbackConfiguration { SizeAndTimeBasedRollingPolicy.class, "setMaxFileSize", String.class); ReflectionUtils.invokeMethod(method, rollingPolicy, maxFileSize); } - int maxHistory = this.patterns.getProperty("logging.file.max-history", - Integer.class, CoreConstants.UNBOUND_HISTORY); - rollingPolicy.setMaxHistory(maxHistory); - appender.setRollingPolicy(rollingPolicy); - rollingPolicy.setParent(appender); - config.start(rollingPolicy); } } 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 046332edef..9db78444bb 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 @@ -128,8 +128,8 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { assertThat(getLineWithText(file, "Hello world")).contains("INFO"); assertThat(ReflectionTestUtils.getField(getRollingPolicy(), "maxFileSize") .toString()).isEqualTo("10 MB"); - assertThat(getRollingPolicy().getMaxHistory()).isEqualTo( - CoreConstants.UNBOUND_HISTORY); + assertThat(getRollingPolicy().getMaxHistory()) + .isEqualTo(CoreConstants.UNBOUND_HISTORY); } @Test @@ -350,8 +350,8 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { public void testMaxFileSizeProperty() throws Exception { MockEnvironment environment = new MockEnvironment(); environment.setProperty("logging.file.max-size", "100MB"); - LoggingInitializationContext loggingInitializationContext = - new LoggingInitializationContext(environment); + 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); @@ -365,8 +365,8 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { public void testMaxHistoryProperty() throws Exception { MockEnvironment environment = new MockEnvironment(); environment.setProperty("logging.file.max-history", "30"); - LoggingInitializationContext loggingInitializationContext = - new LoggingInitializationContext(environment); + 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);