Merge branch '3.1.x'

Closes gh-37446
pull/37451/head
Moritz Halbritter 1 year ago
commit d4fe3bfea7

@ -104,6 +104,7 @@ class DefaultLogbackConfiguration {
ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>(); ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>();
ThresholdFilter filter = new ThresholdFilter(); ThresholdFilter filter = new ThresholdFilter();
filter.setLevel(resolve(config, "${CONSOLE_LOG_THRESHOLD}")); filter.setLevel(resolve(config, "${CONSOLE_LOG_THRESHOLD}"));
filter.start();
appender.addFilter(filter); appender.addFilter(filter);
PatternLayoutEncoder encoder = new PatternLayoutEncoder(); PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setPattern(resolve(config, "${CONSOLE_LOG_PATTERN}")); encoder.setPattern(resolve(config, "${CONSOLE_LOG_PATTERN}"));
@ -118,6 +119,7 @@ class DefaultLogbackConfiguration {
RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>(); RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
ThresholdFilter filter = new ThresholdFilter(); ThresholdFilter filter = new ThresholdFilter();
filter.setLevel(resolve(config, "${FILE_LOG_THRESHOLD}")); filter.setLevel(resolve(config, "${FILE_LOG_THRESHOLD}"));
filter.start();
appender.addFilter(filter); appender.addFilter(filter);
PatternLayoutEncoder encoder = new PatternLayoutEncoder(); PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setPattern(resolve(config, "${FILE_LOG_PATTERN}")); encoder.setPattern(resolve(config, "${FILE_LOG_PATTERN}"));

@ -21,6 +21,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Arrays; import java.util.Arrays;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.HashSet; import java.util.HashSet;
@ -90,6 +91,7 @@ import static org.mockito.Mockito.times;
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Scott Frederick * @author Scott Frederick
* @author Jonatan Ivanov * @author Jonatan Ivanov
* @author Moritz Halbritter
*/ */
@ExtendWith(OutputCaptureExtension.class) @ExtendWith(OutputCaptureExtension.class)
class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
@ -801,6 +803,29 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
.satisfies((ex) -> assertThat(ex.getCause()).isNotInstanceOf(IllegalArgumentException.class)); .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) { private void initialize(LoggingInitializationContext context, String configLocation, LogFile logFile) {
this.loggingSystem.getSystemProperties((ConfigurableEnvironment) context.getEnvironment()).apply(logFile); this.loggingSystem.getSystemProperties((ConfigurableEnvironment) context.getEnvironment()).apply(logFile);
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();

Loading…
Cancel
Save