Skip to content

Commit

Permalink
Switch Logback's file size properties to DataSize
Browse files Browse the repository at this point in the history
This commit changes the target type of file size-based properties to
`DataSize` and tolerates Logback's specific format.

Closes spring-projectsgh-15930
  • Loading branch information
snicoll committed Feb 19, 2019
1 parent e6764bd commit eee07ef
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
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
Expand All @@ -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;

Expand Down Expand Up @@ -145,28 +146,45 @@ private void setRollingPolicy(RollingFileAppender<ILoggingEvent> appender,
"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);
}

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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,23 @@ public void testCleanHistoryOnStartPropertyWithXmlConfiguration() {
}

@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");
Expand All @@ -393,7 +407,7 @@ public void testMaxFileSizeProperty() {
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
Expand Down Expand Up @@ -442,10 +456,23 @@ public void testMaxHistoryPropertyWithXmlConfiguration() throws Exception {
}

@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");
Expand All @@ -454,7 +481,7 @@ public void testTotalSizeCapProperty() {
this.logger.info("Hello world");
assertThat(getLineWithText(file, "Hello world")).contains("INFO");
assertThat(ReflectionTestUtils.getField(getRollingPolicy(), "totalSizeCap")
.toString()).isEqualTo(expectedSize);
.toString()).isEqualTo(expectFileSize);
}

@Test
Expand Down

0 comments on commit eee07ef

Please sign in to comment.