Switch Logback's file size properties to DataSize

This commit changes the target type of file size-based properties to
`DataSize` and tolerates Logback's specific format.

Closes gh-15930
This commit is contained in:
Stephane Nicoll 2019-02-19 11:13:21 +01:00
parent e6764bdc5a
commit eee07efee1
3 changed files with 63 additions and 18 deletions

View File

@ -36,6 +36,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertyResolver;
import org.springframework.core.env.PropertySourcesPropertyResolver;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.unit.DataSize;
/**
* Default logback configuration used by Spring Boot. Uses {@link LogbackConfigurator} to
@ -58,7 +59,7 @@ class DefaultLogbackConfiguration {
private static final String FILE_LOG_PATTERN = "%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} "
+ "${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}";
private static final String MAX_FILE_SIZE = "10MB";
private static final DataSize MAX_FILE_SIZE = DataSize.ofMegabytes(10);
private final PropertyResolver patterns;
@ -145,12 +146,12 @@ class DefaultLogbackConfiguration {
"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));
getDataSize("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",
String.valueOf(CoreConstants.UNBOUNDED_TOTAL_SIZE_CAP))));
DataSize totalSizeCap = getDataSize("logging.file.total-size-cap",
DataSize.ofBytes(CoreConstants.UNBOUNDED_TOTAL_SIZE_CAP));
rollingPolicy.setTotalSizeCap(new FileSize(totalSizeCap.toBytes()));
appender.setRollingPolicy(rollingPolicy);
rollingPolicy.setParent(appender);
config.start(rollingPolicy);
@ -158,15 +159,32 @@ class DefaultLogbackConfiguration {
private void setMaxFileSize(
SizeAndTimeBasedRollingPolicy<ILoggingEvent> rollingPolicy,
String maxFileSize) {
DataSize maxFileSize) {
try {
rollingPolicy.setMaxFileSize(FileSize.valueOf(maxFileSize));
rollingPolicy.setMaxFileSize(new FileSize(maxFileSize.toBytes()));
}
catch (NoSuchMethodError ex) {
// Logback < 1.1.8 used String configuration
Method method = ReflectionUtils.findMethod(
SizeAndTimeBasedRollingPolicy.class, "setMaxFileSize", String.class);
ReflectionUtils.invokeMethod(method, rollingPolicy, maxFileSize);
ReflectionUtils.invokeMethod(method, rollingPolicy,
String.valueOf(maxFileSize.toBytes()));
}
}
private DataSize getDataSize(String property, DataSize defaultSize) {
String value = this.patterns.getProperty(property);
if (value != null) {
try {
return DataSize.parse(value);
}
catch (IllegalArgumentException ex) {
FileSize fileSize = FileSize.valueOf(value);
return DataSize.ofBytes(fileSize.getSize());
}
}
else {
return defaultSize;
}
}

View File

@ -97,7 +97,7 @@
},
{
"name": "logging.file.max-size",
"type": "java.lang.String",
"type": "org.springframework.util.unit.DataSize",
"description": "Maximum log file size. Only supported with the default logback setup.",
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
"defaultValue": "10MB"
@ -111,10 +111,10 @@
},
{
"name": "logging.file.total-size-cap",
"type": "java.lang.String",
"type": "org.springframework.util.unit.DataSize",
"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"
"defaultValue": "0B"
},
{
"name": "logging.group",

View File

@ -382,9 +382,23 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
}
@Test
public void testMaxFileSizeProperty() {
public void testMaxFileSizePropertyWithLogbackFileSize() {
testMaxFileSizeProperty("100 MB", "100 MB");
}
@Test
public void testMaxFileSizePropertyWithDataSize() {
testMaxFileSizeProperty("15MB", "15 MB");
}
@Test
public void testMaxFileSizePropertyWithBytesValue() {
testMaxFileSizeProperty(String.valueOf(10 * 1024 * 1024), "10 MB");
}
private void testMaxFileSizeProperty(String sizeValue, String expectedFileSize) {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.file.max-size", "100MB");
environment.setProperty("logging.file.max-size", sizeValue);
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
environment);
File file = new File(tmpDir(), "logback-test.log");
@ -393,7 +407,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
this.logger.info("Hello world");
assertThat(getLineWithText(file, "Hello world")).contains("INFO");
assertThat(ReflectionTestUtils.getField(getRollingPolicy(), "maxFileSize")
.toString()).isEqualTo("100 MB");
.toString()).isEqualTo(expectedFileSize);
}
@Test
@ -442,10 +456,23 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
}
@Test
public void testTotalSizeCapProperty() {
String expectedSize = "101 MB";
public void testTotalSizeCapPropertyWithLogbackFileSize() {
testTotalSizeCapProperty("101 MB", "101 MB");
}
@Test
public void testTotalSizeCapPropertyWithDataSize() {
testTotalSizeCapProperty("10MB", "10 MB");
}
@Test
public void testTotalSizeCapPropertyWithBytesValue() {
testTotalSizeCapProperty(String.valueOf(10 * 1024 * 1024), "10 MB");
}
private void testTotalSizeCapProperty(String sizeValue, String expectFileSize) {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.file.total-size-cap", expectedSize);
environment.setProperty("logging.file.total-size-cap", sizeValue);
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
environment);
File file = new File(tmpDir(), "logback-test.log");
@ -454,7 +481,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
this.logger.info("Hello world");
assertThat(getLineWithText(file, "Hello world")).contains("INFO");
assertThat(ReflectionTestUtils.getField(getRollingPolicy(), "totalSizeCap")
.toString()).isEqualTo(expectedSize);
.toString()).isEqualTo(expectFileSize);
}
@Test