From 70a992d9a4cdc77ea77f02e9ddf5c75d58da0d4b Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Fri, 5 Apr 2024 13:49:05 +0200 Subject: [PATCH] Convert 'false' to 'OFF' when setting log thresholds Closes gh-40124 --- .../boot/logging/LoggingSystemProperties.java | 28 +++++++++++++++---- .../logging/LoggingSystemPropertiesTests.java | 18 +++++++++++- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java index 17b2e1a3ae..a39c8afbdb 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java @@ -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 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 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 diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java index 2c5a1aaf28..b070493fe6 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java @@ -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)));