Commit f0327fbd authored by Phillip Webb's avatar Phillip Webb

Polish SizeAndTimeBasedRollingPolicy changes

Closes gh-6352
parent 991468b0
...@@ -1624,11 +1624,13 @@ To help with the customization, some other properties are transferred from the S ...@@ -1624,11 +1624,13 @@ To help with the customization, some other properties are transferred from the S
|`logging.file.max-size` |`logging.file.max-size`
|`LOG_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` |`logging.file.max-history`
|`LOG_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` |`logging.path`
|`LOG_PATH` |`LOG_PATH`
...@@ -1655,7 +1657,6 @@ To help with the customization, some other properties are transferred from the S ...@@ -1655,7 +1657,6 @@ To help with the customization, some other properties are transferred from the S
environment variable). environment variable).
|=== |===
All the supported logging systems can consult System properties when parsing their All the supported logging systems can consult System properties when parsing their
configuration files. See the default configurations in `spring-boot.jar` for examples: configuration files. See the default configurations in `spring-boot.jar` for examples:
......
...@@ -149,11 +149,20 @@ class DefaultLogbackConfiguration { ...@@ -149,11 +149,20 @@ class DefaultLogbackConfiguration {
private void setRollingPolicy(RollingFileAppender<ILoggingEvent> appender, private void setRollingPolicy(RollingFileAppender<ILoggingEvent> appender,
LogbackConfigurator config, String logFile) { LogbackConfigurator config, String logFile) {
SizeAndTimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = SizeAndTimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new SizeAndTimeBasedRollingPolicy<>();
new SizeAndTimeBasedRollingPolicy<>();
rollingPolicy.setFileNamePattern(logFile + ".%d{yyyy-MM-dd}.%i.gz"); rollingPolicy.setFileNamePattern(logFile + ".%d{yyyy-MM-dd}.%i.gz");
String maxFileSize = this.patterns.getProperty("logging.file.max-size", setMaxFileSize(rollingPolicy,
MAX_FILE_SIZE); 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<ILoggingEvent> rollingPolicy,
String maxFileSize) {
try { try {
rollingPolicy.setMaxFileSize(FileSize.valueOf(maxFileSize)); rollingPolicy.setMaxFileSize(FileSize.valueOf(maxFileSize));
} }
...@@ -163,12 +172,6 @@ class DefaultLogbackConfiguration { ...@@ -163,12 +172,6 @@ class DefaultLogbackConfiguration {
SizeAndTimeBasedRollingPolicy.class, "setMaxFileSize", String.class); SizeAndTimeBasedRollingPolicy.class, "setMaxFileSize", String.class);
ReflectionUtils.invokeMethod(method, rollingPolicy, maxFileSize); 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);
} }
} }
...@@ -128,8 +128,8 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { ...@@ -128,8 +128,8 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
assertThat(getLineWithText(file, "Hello world")).contains("INFO"); assertThat(getLineWithText(file, "Hello world")).contains("INFO");
assertThat(ReflectionTestUtils.getField(getRollingPolicy(), "maxFileSize") assertThat(ReflectionTestUtils.getField(getRollingPolicy(), "maxFileSize")
.toString()).isEqualTo("10 MB"); .toString()).isEqualTo("10 MB");
assertThat(getRollingPolicy().getMaxHistory()).isEqualTo( assertThat(getRollingPolicy().getMaxHistory())
CoreConstants.UNBOUND_HISTORY); .isEqualTo(CoreConstants.UNBOUND_HISTORY);
} }
@Test @Test
...@@ -350,8 +350,8 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { ...@@ -350,8 +350,8 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
public void testMaxFileSizeProperty() throws Exception { public void testMaxFileSizeProperty() throws Exception {
MockEnvironment environment = new MockEnvironment(); MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.file.max-size", "100MB"); environment.setProperty("logging.file.max-size", "100MB");
LoggingInitializationContext loggingInitializationContext = LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
new LoggingInitializationContext(environment); environment);
File file = new File(tmpDir(), "logback-test.log"); File file = new File(tmpDir(), "logback-test.log");
LogFile logFile = getLogFile(file.getPath(), null); LogFile logFile = getLogFile(file.getPath(), null);
this.loggingSystem.initialize(loggingInitializationContext, null, logFile); this.loggingSystem.initialize(loggingInitializationContext, null, logFile);
...@@ -365,8 +365,8 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { ...@@ -365,8 +365,8 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
public void testMaxHistoryProperty() throws Exception { public void testMaxHistoryProperty() throws Exception {
MockEnvironment environment = new MockEnvironment(); MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.file.max-history", "30"); environment.setProperty("logging.file.max-history", "30");
LoggingInitializationContext loggingInitializationContext = LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
new LoggingInitializationContext(environment); environment);
File file = new File(tmpDir(), "logback-test.log"); File file = new File(tmpDir(), "logback-test.log");
LogFile logFile = getLogFile(file.getPath(), null); LogFile logFile = getLogFile(file.getPath(), null);
this.loggingSystem.initialize(loggingInitializationContext, null, logFile); this.loggingSystem.initialize(loggingInitializationContext, null, logFile);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment