From 59ebc3b3203c6fe84e7442acec401af206775e33 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 20 Nov 2014 10:41:43 +0000 Subject: [PATCH] When Logback is the logging system, ensure that JBoss logging uses it Previously, if Logback was being used as Boot's logging system, but Log4J was also on the classpath before SLF4J and Logback, JBoss Logging would use Log4J for its logging. This lead to warning messages being produced as Log4J was not configured: log4j:WARN No appenders could be found for logger (org.jboss.logging). log4j:WARN Please initialize the log4j system properly. This commit updates LogbackLoggingSystem to set the org.jboss.logging.provider to "slf4j". This ensure that JBoss Logging will use SLF4J and Logback as intended even when Log4J is also on the classpath. Closes gh-1928 --- .../boot/logging/logback/LogbackLoggingSystem.java | 10 ++++++++++ .../logging/logback/LogbackLoggingSystemTests.java | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java b/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java index 8a385dbf2c..85699bef30 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java @@ -43,6 +43,7 @@ import ch.qos.logback.classic.util.ContextInitializer; * * @author Phillip Webb * @author Dave Syer + * @author Andy Wilkinson */ public class LogbackLoggingSystem extends AbstractLoggingSystem { @@ -67,6 +68,11 @@ public class LogbackLoggingSystem extends AbstractLoggingSystem { @Override public void beforeInitialize() { super.beforeInitialize(); + configureJdkLoggingBridgeHandler(); + configureJBossLoggingToUseSlf4j(); + } + + private void configureJdkLoggingBridgeHandler() { try { if (ClassUtils.isPresent("org.slf4j.bridge.SLF4JBridgeHandler", getClassLoader())) { @@ -85,6 +91,10 @@ public class LogbackLoggingSystem extends AbstractLoggingSystem { } } + private void configureJBossLoggingToUseSlf4j() { + System.setProperty("org.jboss.logging.provider", "slf4j"); + } + @Override public void initialize(String configLocation) { Assert.notNull(configLocation, "ConfigLocation must not be null"); diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java index 8a7aacd323..331dacf985 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java @@ -34,6 +34,7 @@ import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; @@ -43,6 +44,7 @@ import static org.junit.Assert.assertTrue; * Tests for {@link LogbackLoggingSystem}. * * @author Dave Syer + * @author Andy Wilkinson */ public class LogbackLoggingSystemTests { @@ -119,4 +121,10 @@ public class LogbackLoggingSystemTests { equalTo(1)); } + @Test + public void jbossLoggingIsConfiguredToUseSlf4j() { + this.loggingSystem.beforeInitialize(); + assertEquals("slf4j", System.getProperty("org.jboss.logging.provider")); + } + }