From af244e1550485b098f45dd3bc84ef2a8317dee5d Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Mon, 18 Sep 2023 14:11:09 +0200 Subject: [PATCH] Start Threshold filters in DefaultLogbackConfiguration Closes gh-36741 --- .../logback/DefaultLogbackConfiguration.java | 2 ++ .../logback/LogbackLoggingSystemTests.java | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java index 0329653359..3e7b38003b 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java @@ -100,6 +100,7 @@ class DefaultLogbackConfiguration { ConsoleAppender appender = new ConsoleAppender<>(); ThresholdFilter filter = new ThresholdFilter(); filter.setLevel(resolve(config, "${CONSOLE_LOG_THRESHOLD}")); + filter.start(); appender.addFilter(filter); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setPattern(resolve(config, "${CONSOLE_LOG_PATTERN}")); @@ -114,6 +115,7 @@ class DefaultLogbackConfiguration { RollingFileAppender appender = new RollingFileAppender<>(); ThresholdFilter filter = new ThresholdFilter(); filter.setLevel(resolve(config, "${FILE_LOG_THRESHOLD}")); + filter.start(); appender.addFilter(filter); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setPattern(resolve(config, "${FILE_LOG_PATTERN}")); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java index c9210207a8..56679821e9 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java @@ -21,6 +21,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.file.Path; import java.util.Arrays; import java.util.EnumSet; import java.util.HashSet; @@ -87,6 +88,7 @@ import static org.mockito.Mockito.times; * @author Robert Thornton * @author EddĂș MelĂ©ndez * @author Scott Frederick + * @author Moritz Halbritter */ @ExtendWith(OutputCaptureExtension.class) class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { @@ -706,6 +708,29 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { .satisfies((ex) -> assertThat(ex.getCause()).isNotInstanceOf(IllegalArgumentException.class)); } + @Test + void shouldRespectConsoleThreshold(CapturedOutput output) { + this.environment.setProperty("logging.threshold.console", "warn"); + this.loggingSystem.beforeInitialize(); + initialize(this.initializationContext, null, null); + this.logger.info("Some info message"); + this.logger.warn("Some warn message"); + assertThat(output).doesNotContain("Some info message").contains("Some warn message"); + } + + @Test + void shouldRespectFileThreshold() { + this.environment.setProperty("logging.threshold.file", "warn"); + this.loggingSystem.beforeInitialize(); + initialize(this.initializationContext, null, getLogFile(null, tmpDir())); + this.logger.info("Some info message"); + this.logger.warn("Some warn message"); + Path file = Path.of(tmpDir(), "spring.log"); + assertThat(file).content(StandardCharsets.UTF_8) + .doesNotContain("Some info message") + .contains("Some warn message"); + } + private void initialize(LoggingInitializationContext context, String configLocation, LogFile logFile) { this.loggingSystem.getSystemProperties((ConfigurableEnvironment) context.getEnvironment()).apply(logFile); this.loggingSystem.initialize(context, configLocation, logFile);