Convert 'false' to 'OFF' when setting log thresholds

Closes gh-40124
This commit is contained in:
Moritz Halbritter 2024-04-05 13:49:05 +02:00
parent fcd41f48cb
commit 70a992d9a4
2 changed files with 40 additions and 6 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -232,8 +232,8 @@ public class LoggingSystemProperties {
setSystemProperty(LoggingSystemProperty.PID, new ApplicationPid().toString());
setSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET, resolver, defaultCharsetName);
setSystemProperty(LoggingSystemProperty.FILE_CHARSET, resolver, defaultCharsetName);
setSystemProperty(LoggingSystemProperty.CONSOLE_THRESHOLD, resolver);
setSystemProperty(LoggingSystemProperty.FILE_THRESHOLD, resolver);
setSystemProperty(LoggingSystemProperty.CONSOLE_THRESHOLD, resolver, this::thresholdMapper);
setSystemProperty(LoggingSystemProperty.FILE_THRESHOLD, resolver, this::thresholdMapper);
setSystemProperty(LoggingSystemProperty.EXCEPTION_CONVERSION_WORD, resolver);
setSystemProperty(LoggingSystemProperty.CONSOLE_PATTERN, resolver);
setSystemProperty(LoggingSystemProperty.FILE_PATTERN, resolver);
@ -256,21 +256,39 @@ public class LoggingSystemProperties {
}
private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver) {
setSystemProperty(property, resolver, null);
setSystemProperty(property, resolver, Function.identity());
}
private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver,
Function<String, String> mapper) {
setSystemProperty(property, resolver, null, mapper);
}
private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver, String defaultValue) {
setSystemProperty(property, resolver, defaultValue, Function.identity());
}
private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver, String defaultValue,
Function<String, String> mapper) {
String value = (property.getApplicationPropertyName() != null)
? resolver.getProperty(property.getApplicationPropertyName()) : null;
value = (value != null) ? value : this.defaultValueResolver.apply(property.getApplicationPropertyName());
value = (value != null) ? value : defaultValue;
setSystemProperty(property.getEnvironmentVariableName(), value);
setSystemProperty(property.getEnvironmentVariableName(), mapper.apply(value));
}
private void setSystemProperty(LoggingSystemProperty property, String value) {
setSystemProperty(property.getEnvironmentVariableName(), value);
}
private String thresholdMapper(String input) {
// YAML converts an unquoted OFF to false
if ("false".equals(input)) {
return "OFF";
}
return input;
}
/**
* Set a system property.
* @param resolver the resolver used to get the property value

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -38,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Andy Wilkinson
* @author Eddú Meléndez
* @author Jonatan Ivanov
* @author Moritz Halbritter
*/
class LoggingSystemPropertiesTests {
@ -155,6 +156,21 @@ class LoggingSystemPropertiesTests {
assertThat(getSystemProperty(LoggingSystemProperty.APPLICATION_NAME)).isNull();
}
@Test
void shouldSupportFalseConsoleThreshold() {
new LoggingSystemProperties(new MockEnvironment().withProperty("logging.threshold.console", "false"))
.apply(null);
assertThat(System.getProperty(LoggingSystemProperty.CONSOLE_THRESHOLD.getEnvironmentVariableName()))
.isEqualTo("OFF");
}
@Test
void shouldSupportFalseFileThreshold() {
new LoggingSystemProperties(new MockEnvironment().withProperty("logging.threshold.file", "false")).apply(null);
assertThat(System.getProperty(LoggingSystemProperty.FILE_THRESHOLD.getEnvironmentVariableName()))
.isEqualTo("OFF");
}
private Environment environment(String key, Object value) {
StandardEnvironment environment = new StandardEnvironment();
environment.getPropertySources().addLast(new MapPropertySource("test", Collections.singletonMap(key, value)));