Skip to content

Commit af244e1

Browse files
committed
Start Threshold filters in DefaultLogbackConfiguration
Closes gh-36741
1 parent 5d120fa commit af244e1

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ private Appender<ILoggingEvent> consoleAppender(LogbackConfigurator config) {
100100
ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>();
101101
ThresholdFilter filter = new ThresholdFilter();
102102
filter.setLevel(resolve(config, "${CONSOLE_LOG_THRESHOLD}"));
103+
filter.start();
103104
appender.addFilter(filter);
104105
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
105106
encoder.setPattern(resolve(config, "${CONSOLE_LOG_PATTERN}"));
@@ -114,6 +115,7 @@ private Appender<ILoggingEvent> fileAppender(LogbackConfigurator config, String
114115
RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
115116
ThresholdFilter filter = new ThresholdFilter();
116117
filter.setLevel(resolve(config, "${FILE_LOG_THRESHOLD}"));
118+
filter.start();
117119
appender.addFilter(filter);
118120
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
119121
encoder.setPattern(resolve(config, "${FILE_LOG_PATTERN}"));

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

+25
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.reflect.Modifier;
2222
import java.nio.charset.Charset;
2323
import java.nio.charset.StandardCharsets;
24+
import java.nio.file.Path;
2425
import java.util.Arrays;
2526
import java.util.EnumSet;
2627
import java.util.HashSet;
@@ -87,6 +88,7 @@
8788
* @author Robert Thornton
8889
* @author Eddú Meléndez
8990
* @author Scott Frederick
91+
* @author Moritz Halbritter
9092
*/
9193
@ExtendWith(OutputCaptureExtension.class)
9294
class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@@ -706,6 +708,29 @@ void whenConfigLocationIsXmlAndHasQueryParametersThenIllegalArgumentExceptionSho
706708
.satisfies((ex) -> assertThat(ex.getCause()).isNotInstanceOf(IllegalArgumentException.class));
707709
}
708710

711+
@Test
712+
void shouldRespectConsoleThreshold(CapturedOutput output) {
713+
this.environment.setProperty("logging.threshold.console", "warn");
714+
this.loggingSystem.beforeInitialize();
715+
initialize(this.initializationContext, null, null);
716+
this.logger.info("Some info message");
717+
this.logger.warn("Some warn message");
718+
assertThat(output).doesNotContain("Some info message").contains("Some warn message");
719+
}
720+
721+
@Test
722+
void shouldRespectFileThreshold() {
723+
this.environment.setProperty("logging.threshold.file", "warn");
724+
this.loggingSystem.beforeInitialize();
725+
initialize(this.initializationContext, null, getLogFile(null, tmpDir()));
726+
this.logger.info("Some info message");
727+
this.logger.warn("Some warn message");
728+
Path file = Path.of(tmpDir(), "spring.log");
729+
assertThat(file).content(StandardCharsets.UTF_8)
730+
.doesNotContain("Some info message")
731+
.contains("Some warn message");
732+
}
733+
709734
private void initialize(LoggingInitializationContext context, String configLocation, LogFile logFile) {
710735
this.loggingSystem.getSystemProperties((ConfigurableEnvironment) context.getEnvironment()).apply(logFile);
711736
this.loggingSystem.initialize(context, configLocation, logFile);

0 commit comments

Comments
 (0)