Skip to content

Commit 70a992d

Browse files
committed
Convert 'false' to 'OFF' when setting log thresholds
Closes gh-40124
1 parent fcd41f4 commit 70a992d

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

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

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -232,8 +232,8 @@ protected void apply(LogFile logFile, PropertyResolver resolver) {
232232
setSystemProperty(LoggingSystemProperty.PID, new ApplicationPid().toString());
233233
setSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET, resolver, defaultCharsetName);
234234
setSystemProperty(LoggingSystemProperty.FILE_CHARSET, resolver, defaultCharsetName);
235-
setSystemProperty(LoggingSystemProperty.CONSOLE_THRESHOLD, resolver);
236-
setSystemProperty(LoggingSystemProperty.FILE_THRESHOLD, resolver);
235+
setSystemProperty(LoggingSystemProperty.CONSOLE_THRESHOLD, resolver, this::thresholdMapper);
236+
setSystemProperty(LoggingSystemProperty.FILE_THRESHOLD, resolver, this::thresholdMapper);
237237
setSystemProperty(LoggingSystemProperty.EXCEPTION_CONVERSION_WORD, resolver);
238238
setSystemProperty(LoggingSystemProperty.CONSOLE_PATTERN, resolver);
239239
setSystemProperty(LoggingSystemProperty.FILE_PATTERN, resolver);
@@ -256,21 +256,39 @@ private void setApplicationNameSystemProperty(PropertyResolver resolver) {
256256
}
257257

258258
private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver) {
259-
setSystemProperty(property, resolver, null);
259+
setSystemProperty(property, resolver, Function.identity());
260+
}
261+
262+
private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver,
263+
Function<String, String> mapper) {
264+
setSystemProperty(property, resolver, null, mapper);
260265
}
261266

262267
private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver, String defaultValue) {
268+
setSystemProperty(property, resolver, defaultValue, Function.identity());
269+
}
270+
271+
private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver, String defaultValue,
272+
Function<String, String> mapper) {
263273
String value = (property.getApplicationPropertyName() != null)
264274
? resolver.getProperty(property.getApplicationPropertyName()) : null;
265275
value = (value != null) ? value : this.defaultValueResolver.apply(property.getApplicationPropertyName());
266276
value = (value != null) ? value : defaultValue;
267-
setSystemProperty(property.getEnvironmentVariableName(), value);
277+
setSystemProperty(property.getEnvironmentVariableName(), mapper.apply(value));
268278
}
269279

270280
private void setSystemProperty(LoggingSystemProperty property, String value) {
271281
setSystemProperty(property.getEnvironmentVariableName(), value);
272282
}
273283

284+
private String thresholdMapper(String input) {
285+
// YAML converts an unquoted OFF to false
286+
if ("false".equals(input)) {
287+
return "OFF";
288+
}
289+
return input;
290+
}
291+
274292
/**
275293
* Set a system property.
276294
* @param resolver the resolver used to get the property value

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@
3838
* @author Andy Wilkinson
3939
* @author Eddú Meléndez
4040
* @author Jonatan Ivanov
41+
* @author Moritz Halbritter
4142
*/
4243
class LoggingSystemPropertiesTests {
4344

@@ -155,6 +156,21 @@ void loggedApplicationNameWhenApplicationNameLoggingDisabled() {
155156
assertThat(getSystemProperty(LoggingSystemProperty.APPLICATION_NAME)).isNull();
156157
}
157158

159+
@Test
160+
void shouldSupportFalseConsoleThreshold() {
161+
new LoggingSystemProperties(new MockEnvironment().withProperty("logging.threshold.console", "false"))
162+
.apply(null);
163+
assertThat(System.getProperty(LoggingSystemProperty.CONSOLE_THRESHOLD.getEnvironmentVariableName()))
164+
.isEqualTo("OFF");
165+
}
166+
167+
@Test
168+
void shouldSupportFalseFileThreshold() {
169+
new LoggingSystemProperties(new MockEnvironment().withProperty("logging.threshold.file", "false")).apply(null);
170+
assertThat(System.getProperty(LoggingSystemProperty.FILE_THRESHOLD.getEnvironmentVariableName()))
171+
.isEqualTo("OFF");
172+
}
173+
158174
private Environment environment(String key, Object value) {
159175
StandardEnvironment environment = new StandardEnvironment();
160176
environment.getPropertySources().addLast(new MapPropertySource("test", Collections.singletonMap(key, value)));

0 commit comments

Comments
 (0)