From b5bdee24c2793464a61e31d0e486f025ad9d0fdf Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 14 Nov 2022 18:03:29 +0000 Subject: [PATCH] When Logback's in use, route JBoss Logging straight into SLF4J Prior to this change, when using spring-boot-starter-logging, JBoss Logging would route like this: JBoss Logging -> Log4j2 API -> SLF4J -> Logback This is inefficient as there's no need for it to go via the Log4j2 API. This commit updates the Logback logging system to configure a system property that controls JBoss's routing. With this change in place, JBoss Logging will route like this: JBoss Logging -> SLF4J -> Logback Closes gh-33155 --- .../LogbackLoggingSystemProperties.java | 15 +++++++++++++++ .../logback/LogbackLoggingSystemTests.java | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystemProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystemProperties.java index 74adcd51da8e..9aa2ba5620c5 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystemProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystemProperties.java @@ -20,6 +20,7 @@ import java.util.function.BiConsumer; import ch.qos.logback.core.util.FileSize; +import org.flywaydb.core.internal.util.ClassUtils; import org.springframework.boot.logging.LogFile; import org.springframework.boot.logging.LoggingSystemProperties; @@ -37,6 +38,9 @@ */ public class LogbackLoggingSystemProperties extends LoggingSystemProperties { + private static final boolean JBOSS_LOGGING_PRESENT = ClassUtils.isPresent("org.jboss.logging.Logger", + LogbackLoggingSystemProperties.class.getClassLoader()); + /** * The name of the System property that contains the rolled-over log file name * pattern. @@ -85,6 +89,17 @@ protected Charset getDefaultCharset() { @Override protected void apply(LogFile logFile, PropertyResolver resolver) { super.apply(logFile, resolver); + applyJBossLoggingProperties(); + applyRollingPolicyProperties(resolver); + } + + private void applyJBossLoggingProperties() { + if (JBOSS_LOGGING_PRESENT) { + setSystemProperty("org.jboss.logging.provider", "slf4j"); + } + } + + private void applyRollingPolicyProperties(PropertyResolver resolver) { applyRollingPolicy(resolver, ROLLINGPOLICY_FILE_NAME_PATTERN, "logging.logback.rollingpolicy.file-name-pattern", "logging.pattern.rolling-file-name"); applyRollingPolicy(resolver, ROLLINGPOLICY_CLEAN_HISTORY_ON_START, 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 5871e5885302..cba6f68e7e3a 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 @@ -55,6 +55,7 @@ import org.springframework.boot.logging.LoggingInitializationContext; import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.logging.LoggingSystemProperties; +import org.springframework.boot.testsupport.classpath.ClassPathOverrides; import org.springframework.boot.testsupport.system.CapturedOutput; import org.springframework.boot.testsupport.system.OutputCaptureExtension; import org.springframework.core.convert.ConversionService; @@ -118,6 +119,22 @@ void cleanUp() { ((LoggerContext) LoggerFactory.getILoggerFactory()).stop(); } + @Test + @ClassPathOverrides("org.jboss.logging:jboss-logging:3.5.0.Final") + void jbossLoggingRoutesThroughLog4j2ByDefault() { + org.jboss.logging.Logger jbossLogger = org.jboss.logging.Logger.getLogger(getClass()); + assertThat(jbossLogger.getClass().getName()).isEqualTo("org.jboss.logging.Log4j2Logger"); + } + + @Test + @ClassPathOverrides("org.jboss.logging:jboss-logging:3.5.0.Final") + void jbossLoggingRoutesThroughSlf4jWhenLoggingSystemIsInitialized() { + this.loggingSystem.beforeInitialize(); + initialize(this.initializationContext, null, null); + assertThat(org.jboss.logging.Logger.getLogger(getClass()).getClass().getName()) + .isEqualTo("org.jboss.logging.Slf4jLocationAwareLogger"); + } + @Test void noFile(CapturedOutput output) { this.loggingSystem.beforeInitialize(); @@ -523,6 +540,7 @@ void initializeShouldApplyLogbackSystemPropertiesToTheContext() { ReflectionUtils.doWithFields(LogbackLoggingSystemProperties.class, (field) -> expectedProperties.add((String) field.get(null)), this::isPublicStaticFinal); expectedProperties.removeAll(Arrays.asList("LOG_FILE", "LOG_PATH")); + expectedProperties.add("org.jboss.logging.provider"); assertThat(properties).containsOnlyKeys(expectedProperties); assertThat(properties).containsEntry("CONSOLE_LOG_CHARSET", Charset.defaultCharset().name()); }