Skip to content

Commit

Permalink
Start Threshold filters in DefaultLogbackConfiguration
Browse files Browse the repository at this point in the history
Closes gh-36741
  • Loading branch information
mhalbritter committed Sep 18, 2023
1 parent 5d120fa commit af244e1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ private Appender<ILoggingEvent> consoleAppender(LogbackConfigurator config) {
ConsoleAppender<ILoggingEvent> 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}"));
Expand All @@ -114,6 +115,7 @@ private Appender<ILoggingEvent> fileAppender(LogbackConfigurator config, String
RollingFileAppender<ILoggingEvent> 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}"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -87,6 +88,7 @@
* @author Robert Thornton
* @author Eddú Meléndez
* @author Scott Frederick
* @author Moritz Halbritter
*/
@ExtendWith(OutputCaptureExtension.class)
class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
Expand Down Expand Up @@ -706,6 +708,29 @@ void whenConfigLocationIsXmlAndHasQueryParametersThenIllegalArgumentExceptionSho
.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);
Expand Down

0 comments on commit af244e1

Please sign in to comment.