Skip to content

Commit eee07ef

Browse files
committed
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
1 parent e6764bd commit eee07ef

File tree

3 files changed

+63
-18
lines changed

3 files changed

+63
-18
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.core.env.PropertyResolver;
3737
import org.springframework.core.env.PropertySourcesPropertyResolver;
3838
import org.springframework.util.ReflectionUtils;
39+
import org.springframework.util.unit.DataSize;
3940

4041
/**
4142
* Default logback configuration used by Spring Boot. Uses {@link LogbackConfigurator} to
@@ -58,7 +59,7 @@ class DefaultLogbackConfiguration {
5859
private static final String FILE_LOG_PATTERN = "%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} "
5960
+ "${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}";
6061

61-
private static final String MAX_FILE_SIZE = "10MB";
62+
private static final DataSize MAX_FILE_SIZE = DataSize.ofMegabytes(10);
6263

6364
private final PropertyResolver patterns;
6465

@@ -145,28 +146,45 @@ private void setRollingPolicy(RollingFileAppender<ILoggingEvent> appender,
145146
"logging.file.clean-history-on-start", Boolean.class, false));
146147
rollingPolicy.setFileNamePattern(logFile + ".%d{yyyy-MM-dd}.%i.gz");
147148
setMaxFileSize(rollingPolicy,
148-
this.patterns.getProperty("logging.file.max-size", MAX_FILE_SIZE));
149+
getDataSize("logging.file.max-size", MAX_FILE_SIZE));
149150
rollingPolicy.setMaxHistory(this.patterns.getProperty("logging.file.max-history",
150151
Integer.class, CoreConstants.UNBOUND_HISTORY));
151-
rollingPolicy.setTotalSizeCap(
152-
FileSize.valueOf(this.patterns.getProperty("logging.file.total-size-cap",
153-
String.valueOf(CoreConstants.UNBOUNDED_TOTAL_SIZE_CAP))));
152+
DataSize totalSizeCap = getDataSize("logging.file.total-size-cap",
153+
DataSize.ofBytes(CoreConstants.UNBOUNDED_TOTAL_SIZE_CAP));
154+
rollingPolicy.setTotalSizeCap(new FileSize(totalSizeCap.toBytes()));
154155
appender.setRollingPolicy(rollingPolicy);
155156
rollingPolicy.setParent(appender);
156157
config.start(rollingPolicy);
157158
}
158159

159160
private void setMaxFileSize(
160161
SizeAndTimeBasedRollingPolicy<ILoggingEvent> rollingPolicy,
161-
String maxFileSize) {
162+
DataSize maxFileSize) {
162163
try {
163-
rollingPolicy.setMaxFileSize(FileSize.valueOf(maxFileSize));
164+
rollingPolicy.setMaxFileSize(new FileSize(maxFileSize.toBytes()));
164165
}
165166
catch (NoSuchMethodError ex) {
166167
// Logback < 1.1.8 used String configuration
167168
Method method = ReflectionUtils.findMethod(
168169
SizeAndTimeBasedRollingPolicy.class, "setMaxFileSize", String.class);
169-
ReflectionUtils.invokeMethod(method, rollingPolicy, maxFileSize);
170+
ReflectionUtils.invokeMethod(method, rollingPolicy,
171+
String.valueOf(maxFileSize.toBytes()));
172+
}
173+
}
174+
175+
private DataSize getDataSize(String property, DataSize defaultSize) {
176+
String value = this.patterns.getProperty(property);
177+
if (value != null) {
178+
try {
179+
return DataSize.parse(value);
180+
}
181+
catch (IllegalArgumentException ex) {
182+
FileSize fileSize = FileSize.valueOf(value);
183+
return DataSize.ofBytes(fileSize.getSize());
184+
}
185+
}
186+
else {
187+
return defaultSize;
170188
}
171189
}
172190

spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
},
9898
{
9999
"name": "logging.file.max-size",
100-
"type": "java.lang.String",
100+
"type": "org.springframework.util.unit.DataSize",
101101
"description": "Maximum log file size. Only supported with the default logback setup.",
102102
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
103103
"defaultValue": "10MB"
@@ -111,10 +111,10 @@
111111
},
112112
{
113113
"name": "logging.file.total-size-cap",
114-
"type": "java.lang.String",
114+
"type": "org.springframework.util.unit.DataSize",
115115
"description": "Total size of log backups to be kept. Only supported with the default logback setup.",
116116
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
117-
"defaultValue": "0"
117+
"defaultValue": "0B"
118118
},
119119
{
120120
"name": "logging.group",

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,23 @@ public void testCleanHistoryOnStartPropertyWithXmlConfiguration() {
382382
}
383383

384384
@Test
385-
public void testMaxFileSizeProperty() {
385+
public void testMaxFileSizePropertyWithLogbackFileSize() {
386+
testMaxFileSizeProperty("100 MB", "100 MB");
387+
}
388+
389+
@Test
390+
public void testMaxFileSizePropertyWithDataSize() {
391+
testMaxFileSizeProperty("15MB", "15 MB");
392+
}
393+
394+
@Test
395+
public void testMaxFileSizePropertyWithBytesValue() {
396+
testMaxFileSizeProperty(String.valueOf(10 * 1024 * 1024), "10 MB");
397+
}
398+
399+
private void testMaxFileSizeProperty(String sizeValue, String expectedFileSize) {
386400
MockEnvironment environment = new MockEnvironment();
387-
environment.setProperty("logging.file.max-size", "100MB");
401+
environment.setProperty("logging.file.max-size", sizeValue);
388402
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
389403
environment);
390404
File file = new File(tmpDir(), "logback-test.log");
@@ -393,7 +407,7 @@ public void testMaxFileSizeProperty() {
393407
this.logger.info("Hello world");
394408
assertThat(getLineWithText(file, "Hello world")).contains("INFO");
395409
assertThat(ReflectionTestUtils.getField(getRollingPolicy(), "maxFileSize")
396-
.toString()).isEqualTo("100 MB");
410+
.toString()).isEqualTo(expectedFileSize);
397411
}
398412

399413
@Test
@@ -442,10 +456,23 @@ public void testMaxHistoryPropertyWithXmlConfiguration() throws Exception {
442456
}
443457

444458
@Test
445-
public void testTotalSizeCapProperty() {
446-
String expectedSize = "101 MB";
459+
public void testTotalSizeCapPropertyWithLogbackFileSize() {
460+
testTotalSizeCapProperty("101 MB", "101 MB");
461+
}
462+
463+
@Test
464+
public void testTotalSizeCapPropertyWithDataSize() {
465+
testTotalSizeCapProperty("10MB", "10 MB");
466+
}
467+
468+
@Test
469+
public void testTotalSizeCapPropertyWithBytesValue() {
470+
testTotalSizeCapProperty(String.valueOf(10 * 1024 * 1024), "10 MB");
471+
}
472+
473+
private void testTotalSizeCapProperty(String sizeValue, String expectFileSize) {
447474
MockEnvironment environment = new MockEnvironment();
448-
environment.setProperty("logging.file.total-size-cap", expectedSize);
475+
environment.setProperty("logging.file.total-size-cap", sizeValue);
449476
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
450477
environment);
451478
File file = new File(tmpDir(), "logback-test.log");
@@ -454,7 +481,7 @@ public void testTotalSizeCapProperty() {
454481
this.logger.info("Hello world");
455482
assertThat(getLineWithText(file, "Hello world")).contains("INFO");
456483
assertThat(ReflectionTestUtils.getField(getRollingPolicy(), "totalSizeCap")
457-
.toString()).isEqualTo(expectedSize);
484+
.toString()).isEqualTo(expectFileSize);
458485
}
459486

460487
@Test

0 commit comments

Comments
 (0)