Merge branch '3.1.x'

Closes gh-37601
pull/37603/head
Andy Wilkinson 1 year ago
commit 145fb161a1

@ -30,6 +30,7 @@ import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator; import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.classic.jul.LevelChangePropagator; import ch.qos.logback.classic.jul.LevelChangePropagator;
import ch.qos.logback.classic.spi.TurboFilterList;
import ch.qos.logback.classic.turbo.TurboFilter; import ch.qos.logback.classic.turbo.TurboFilter;
import ch.qos.logback.core.joran.spi.JoranException; import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.spi.FilterReply; import ch.qos.logback.core.spi.FilterReply;
@ -219,6 +220,7 @@ public class LogbackLoggingSystem extends AbstractLoggingSystem implements BeanF
protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) { protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) {
LoggerContext context = getLoggerContext(); LoggerContext context = getLoggerContext();
stopAndReset(context); stopAndReset(context);
withLoggingSuppressed(() -> {
boolean debug = Boolean.getBoolean("logback.debug"); boolean debug = Boolean.getBoolean("logback.debug");
if (debug) { if (debug) {
StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener()); StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener());
@ -231,22 +233,25 @@ public class LogbackLoggingSystem extends AbstractLoggingSystem implements BeanF
: new LogbackConfigurator(context); : new LogbackConfigurator(context);
new DefaultLogbackConfiguration(logFile).apply(configurator); new DefaultLogbackConfiguration(logFile).apply(configurator);
context.setPackagingDataEnabled(true); context.setPackagingDataEnabled(true);
});
} }
@Override @Override
protected void loadConfiguration(LoggingInitializationContext initializationContext, String location, protected void loadConfiguration(LoggingInitializationContext initializationContext, String location,
LogFile logFile) { LogFile logFile) {
LoggerContext loggerContext = getLoggerContext();
stopAndReset(loggerContext);
withLoggingSuppressed(() -> {
if (initializationContext != null) { if (initializationContext != null) {
applySystemProperties(initializationContext.getEnvironment(), logFile); applySystemProperties(initializationContext.getEnvironment(), logFile);
} }
LoggerContext loggerContext = getLoggerContext();
stopAndReset(loggerContext);
try { try {
configureByResourceUrl(initializationContext, loggerContext, ResourceUtils.getURL(location)); configureByResourceUrl(initializationContext, loggerContext, ResourceUtils.getURL(location));
} }
catch (Exception ex) { catch (Exception ex) {
throw new IllegalStateException("Could not initialize Logback logging from " + location, ex); throw new IllegalStateException("Could not initialize Logback logging from " + location, ex);
} }
});
reportConfigurationErrorsIfNecessary(loggerContext); reportConfigurationErrorsIfNecessary(loggerContext);
} }
@ -455,6 +460,17 @@ public class LogbackLoggingSystem extends AbstractLoggingSystem implements BeanF
return contribution; return contribution;
} }
private void withLoggingSuppressed(Runnable action) {
TurboFilterList turboFilters = getLoggerContext().getTurboFilterList();
turboFilters.add(FILTER);
try {
action.run();
}
finally {
turboFilters.remove(FILTER);
}
}
/** /**
* {@link LoggingSystemFactory} that returns {@link LogbackLoggingSystem} if possible. * {@link LoggingSystemFactory} that returns {@link LogbackLoggingSystem} if possible.
*/ */

@ -60,12 +60,14 @@ import org.springframework.boot.logging.LoggingInitializationContext;
import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.logging.LoggingSystem;
import org.springframework.boot.logging.LoggingSystemProperties; import org.springframework.boot.logging.LoggingSystemProperties;
import org.springframework.boot.logging.LoggingSystemProperty; import org.springframework.boot.logging.LoggingSystemProperty;
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.classpath.ClassPathOverrides; import org.springframework.boot.testsupport.classpath.ClassPathOverrides;
import org.springframework.boot.testsupport.system.CapturedOutput; import org.springframework.boot.testsupport.system.CapturedOutput;
import org.springframework.boot.testsupport.system.OutputCaptureExtension; import org.springframework.boot.testsupport.system.OutputCaptureExtension;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.ConfigurableConversionService; import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
@ -94,6 +96,7 @@ import static org.mockito.Mockito.times;
* @author Moritz Halbritter * @author Moritz Halbritter
*/ */
@ExtendWith(OutputCaptureExtension.class) @ExtendWith(OutputCaptureExtension.class)
@ClassPathExclusions({ "log4j-core-*.jar", "log4j-api-*.jar" })
class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
private final LogbackLoggingSystem loggingSystem = new LogbackLoggingSystem(getClass().getClassLoader()); private final LogbackLoggingSystem loggingSystem = new LogbackLoggingSystem(getClass().getClassLoader());
@ -129,7 +132,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
} }
@Test @Test
@ClassPathOverrides("org.jboss.logging:jboss-logging:3.5.0.Final") @ClassPathOverrides({ "org.jboss.logging:jboss-logging:3.5.0.Final", "org.apache.logging.log4j:log4j-core:2.19.0" })
void jbossLoggingRoutesThroughLog4j2ByDefault() { void jbossLoggingRoutesThroughLog4j2ByDefault() {
System.getProperties().remove("org.jboss.logging.provider"); System.getProperties().remove("org.jboss.logging.provider");
org.jboss.logging.Logger jbossLogger = org.jboss.logging.Logger.getLogger(getClass()); org.jboss.logging.Logger jbossLogger = org.jboss.logging.Logger.getLogger(getClass());
@ -826,6 +829,15 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
.contains("Some warn message"); .contains("Some warn message");
} }
@Test
void applyingSystemPropertiesDoesNotCauseUnwantedStatusWarnings(CapturedOutput output) {
this.loggingSystem.beforeInitialize();
this.environment.getPropertySources()
.addFirst(new MapPropertySource("test", Map.of("logging.pattern.console", "[CONSOLE]%m")));
this.loggingSystem.initialize(this.initializationContext, "classpath:logback-nondefault.xml", null);
assertThat(output).doesNotContain("WARN");
}
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