mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Polish SizeAndTimeBasedRollingPolicy changes
Closes gh-6352
This commit is contained in:
parent
991468b0ef
commit
f0327fbd0d
@ -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:
|
||||
|
||||
|
@ -149,11 +149,20 @@ class DefaultLogbackConfiguration {
|
||||
|
||||
private void setRollingPolicy(RollingFileAppender<ILoggingEvent> appender,
|
||||
LogbackConfigurator config, String logFile) {
|
||||
SizeAndTimeBasedRollingPolicy<ILoggingEvent> rollingPolicy =
|
||||
new SizeAndTimeBasedRollingPolicy<>();
|
||||
SizeAndTimeBasedRollingPolicy<ILoggingEvent> 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<ILoggingEvent> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user