Commit eee07efe authored by Stephane Nicoll's avatar Stephane Nicoll

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
parent e6764bdc
...@@ -36,6 +36,7 @@ import org.springframework.core.env.Environment; ...@@ -36,6 +36,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertyResolver; import org.springframework.core.env.PropertyResolver;
import org.springframework.core.env.PropertySourcesPropertyResolver; import org.springframework.core.env.PropertySourcesPropertyResolver;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.util.unit.DataSize;
/** /**
* Default logback configuration used by Spring Boot. Uses {@link LogbackConfigurator} to * Default logback configuration used by Spring Boot. Uses {@link LogbackConfigurator} to
...@@ -58,7 +59,7 @@ class DefaultLogbackConfiguration { ...@@ -58,7 +59,7 @@ class DefaultLogbackConfiguration {
private static final String FILE_LOG_PATTERN = "%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} " 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}"; + "${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; private final PropertyResolver patterns;
...@@ -145,12 +146,12 @@ class DefaultLogbackConfiguration { ...@@ -145,12 +146,12 @@ class DefaultLogbackConfiguration {
"logging.file.clean-history-on-start", Boolean.class, false)); "logging.file.clean-history-on-start", Boolean.class, false));
rollingPolicy.setFileNamePattern(logFile + ".%d{yyyy-MM-dd}.%i.gz"); rollingPolicy.setFileNamePattern(logFile + ".%d{yyyy-MM-dd}.%i.gz");
setMaxFileSize(rollingPolicy, 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", rollingPolicy.setMaxHistory(this.patterns.getProperty("logging.file.max-history",
Integer.class, CoreConstants.UNBOUND_HISTORY)); Integer.class, CoreConstants.UNBOUND_HISTORY));
rollingPolicy.setTotalSizeCap( DataSize totalSizeCap = getDataSize("logging.file.total-size-cap",
FileSize.valueOf(this.patterns.getProperty("logging.file.total-size-cap", DataSize.ofBytes(CoreConstants.UNBOUNDED_TOTAL_SIZE_CAP));
String.valueOf(CoreConstants.UNBOUNDED_TOTAL_SIZE_CAP)))); rollingPolicy.setTotalSizeCap(new FileSize(totalSizeCap.toBytes()));
appender.setRollingPolicy(rollingPolicy); appender.setRollingPolicy(rollingPolicy);
rollingPolicy.setParent(appender); rollingPolicy.setParent(appender);
config.start(rollingPolicy); config.start(rollingPolicy);
...@@ -158,15 +159,32 @@ class DefaultLogbackConfiguration { ...@@ -158,15 +159,32 @@ class DefaultLogbackConfiguration {
private void setMaxFileSize( private void setMaxFileSize(
SizeAndTimeBasedRollingPolicy<ILoggingEvent> rollingPolicy, SizeAndTimeBasedRollingPolicy<ILoggingEvent> rollingPolicy,
String maxFileSize) { DataSize maxFileSize) {
try { try {
rollingPolicy.setMaxFileSize(FileSize.valueOf(maxFileSize)); rollingPolicy.setMaxFileSize(new FileSize(maxFileSize.toBytes()));
} }
catch (NoSuchMethodError ex) { catch (NoSuchMethodError ex) {
// Logback < 1.1.8 used String configuration // Logback < 1.1.8 used String configuration
Method method = ReflectionUtils.findMethod( Method method = ReflectionUtils.findMethod(
SizeAndTimeBasedRollingPolicy.class, "setMaxFileSize", String.class); 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;
} }
} }
......
...@@ -97,7 +97,7 @@ ...@@ -97,7 +97,7 @@
}, },
{ {
"name": "logging.file.max-size", "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.", "description": "Maximum log file size. Only supported with the default logback setup.",
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener", "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
"defaultValue": "10MB" "defaultValue": "10MB"
...@@ -111,10 +111,10 @@ ...@@ -111,10 +111,10 @@
}, },
{ {
"name": "logging.file.total-size-cap", "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.", "description": "Total size of log backups to be kept. Only supported with the default logback setup.",
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener", "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
"defaultValue": "0" "defaultValue": "0B"
}, },
{ {
"name": "logging.group", "name": "logging.group",
......
...@@ -382,9 +382,23 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { ...@@ -382,9 +382,23 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
} }
@Test @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(); MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.file.max-size", "100MB"); environment.setProperty("logging.file.max-size", sizeValue);
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext( LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
environment); environment);
File file = new File(tmpDir(), "logback-test.log"); File file = new File(tmpDir(), "logback-test.log");
...@@ -393,7 +407,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { ...@@ -393,7 +407,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
this.logger.info("Hello world"); this.logger.info("Hello world");
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("100 MB"); .toString()).isEqualTo(expectedFileSize);
} }
@Test @Test
...@@ -442,10 +456,23 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { ...@@ -442,10 +456,23 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
} }
@Test @Test
public void testTotalSizeCapProperty() { public void testTotalSizeCapPropertyWithLogbackFileSize() {
String expectedSize = "101 MB"; 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(); MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.file.total-size-cap", expectedSize); environment.setProperty("logging.file.total-size-cap", sizeValue);
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext( LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
environment); environment);
File file = new File(tmpDir(), "logback-test.log"); File file = new File(tmpDir(), "logback-test.log");
...@@ -454,7 +481,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { ...@@ -454,7 +481,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
this.logger.info("Hello world"); this.logger.info("Hello world");
assertThat(getLineWithText(file, "Hello world")).contains("INFO"); assertThat(getLineWithText(file, "Hello world")).contains("INFO");
assertThat(ReflectionTestUtils.getField(getRollingPolicy(), "totalSizeCap") assertThat(ReflectionTestUtils.getField(getRollingPolicy(), "totalSizeCap")
.toString()).isEqualTo(expectedSize); .toString()).isEqualTo(expectFileSize);
} }
@Test @Test
......
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