Skip to content

Commit

Permalink
Propagates logback log levels to java.util.logging
Browse files Browse the repository at this point in the history
Adds the LevelChangePropagator logback listener in order to propagate
Logback's log level changes to java.util.logging loggers.

Logback documentation :
http://logback.qos.ch/manual/configuration.html#LevelChangePropagator

Fixes gh-3924
Closes gh-3926
  • Loading branch information
Thomas Recloux authored and philwebb committed Sep 10, 2015
1 parent e04fb15 commit 22e0a50
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
Expand Up @@ -46,7 +46,7 @@ public void cleanUp() {

private void configureJdkLoggingBridgeHandler() {
try {
if (bridgeHandlerIsAvailable()) {
if (isBridgeHandlerAvailable()) {
removeJdkLoggingBridgeHandler();
SLF4JBridgeHandler.install();
}
Expand All @@ -56,13 +56,13 @@ private void configureJdkLoggingBridgeHandler() {
}
}

private boolean bridgeHandlerIsAvailable() {
protected final boolean isBridgeHandlerAvailable() {
return ClassUtils.isPresent(BRIDGE_HANDLER, getClassLoader());
}

private void removeJdkLoggingBridgeHandler() {
try {
if (bridgeHandlerIsAvailable()) {
if (isBridgeHandlerAvailable()) {
try {
SLF4JBridgeHandler.removeHandlersForRootLogger();
}
Expand Down
Expand Up @@ -37,6 +37,7 @@

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.jul.LevelChangePropagator;
import ch.qos.logback.classic.turbo.TurboFilter;
import ch.qos.logback.classic.util.ContextInitializer;
import ch.qos.logback.core.spi.FilterReply;
Expand Down Expand Up @@ -99,8 +100,7 @@ public void initialize(String configLocation, LogFile logFile) {
@Override
protected void loadDefaults(LogFile logFile) {
LoggerContext context = getLoggerContext();
context.stop();
context.reset();
stopAndReset(context);
LogbackConfigurator configurator = new LogbackConfigurator(context);
new DefaultLogbackConfiguration(logFile).apply(configurator);
}
Expand All @@ -112,8 +112,7 @@ protected void loadConfiguration(String location, LogFile logFile) {
logFile.applyToSystemProperties();
}
LoggerContext context = getLoggerContext();
context.stop();
context.reset();
stopAndReset(context);
try {
URL url = ResourceUtils.getURL(location);
new ContextInitializer(context).configureByResource(url);
Expand All @@ -124,6 +123,21 @@ protected void loadConfiguration(String location, LogFile logFile) {
}
}

private void stopAndReset(LoggerContext loggerContext) {
loggerContext.stop();
loggerContext.reset();
if (isBridgeHandlerAvailable()) {
addLevelChangePropagator(loggerContext);
}
}

private void addLevelChangePropagator(LoggerContext loggerContext) {
LevelChangePropagator levelChangePropagator = new LevelChangePropagator();
levelChangePropagator.setResetJUL(true);
levelChangePropagator.setContext(loggerContext);
loggerContext.addListener(levelChangePropagator);
}

@Override
protected void reinitialize() {
getLoggerContext().reset();
Expand Down
Expand Up @@ -146,6 +146,18 @@ public void loggingThatUsesJulIsCaptured() {
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
}

@Test
public void loggingLevelIsPropagatedToJulI() {
this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(null, null);
this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG);
java.util.logging.Logger julLogger = java.util.logging.Logger
.getLogger(getClass().getName());
julLogger.fine("Hello debug world");
String output = this.output.toString().trim();
assertTrue("Wrong output:\n" + output, output.contains("Hello debug world"));
}

@Test
public void jbossLoggingIsConfiguredToUseSlf4j() {
this.loggingSystem.beforeInitialize();
Expand Down

0 comments on commit 22e0a50

Please sign in to comment.