mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Merge pull request #3926 from trecloux/gh-3924_propagate-log-level-to-JUL
* gh-3926: Propagates logback log levels to java.util.logging
This commit is contained in:
commit
e69a5879b0
@ -46,7 +46,7 @@ public abstract class Slf4JLoggingSystem extends AbstractLoggingSystem {
|
|||||||
|
|
||||||
private void configureJdkLoggingBridgeHandler() {
|
private void configureJdkLoggingBridgeHandler() {
|
||||||
try {
|
try {
|
||||||
if (bridgeHandlerIsAvailable()) {
|
if (isBridgeHandlerAvailable()) {
|
||||||
removeJdkLoggingBridgeHandler();
|
removeJdkLoggingBridgeHandler();
|
||||||
SLF4JBridgeHandler.install();
|
SLF4JBridgeHandler.install();
|
||||||
}
|
}
|
||||||
@ -56,13 +56,13 @@ public abstract class Slf4JLoggingSystem extends AbstractLoggingSystem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean bridgeHandlerIsAvailable() {
|
protected final boolean isBridgeHandlerAvailable() {
|
||||||
return ClassUtils.isPresent(BRIDGE_HANDLER, getClassLoader());
|
return ClassUtils.isPresent(BRIDGE_HANDLER, getClassLoader());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeJdkLoggingBridgeHandler() {
|
private void removeJdkLoggingBridgeHandler() {
|
||||||
try {
|
try {
|
||||||
if (bridgeHandlerIsAvailable()) {
|
if (isBridgeHandlerAvailable()) {
|
||||||
try {
|
try {
|
||||||
SLF4JBridgeHandler.removeHandlersForRootLogger();
|
SLF4JBridgeHandler.removeHandlersForRootLogger();
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@ import org.springframework.util.StringUtils;
|
|||||||
|
|
||||||
import ch.qos.logback.classic.Level;
|
import ch.qos.logback.classic.Level;
|
||||||
import ch.qos.logback.classic.LoggerContext;
|
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.turbo.TurboFilter;
|
||||||
import ch.qos.logback.classic.util.ContextInitializer;
|
import ch.qos.logback.classic.util.ContextInitializer;
|
||||||
import ch.qos.logback.core.spi.FilterReply;
|
import ch.qos.logback.core.spi.FilterReply;
|
||||||
@ -99,8 +100,7 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem {
|
|||||||
@Override
|
@Override
|
||||||
protected void loadDefaults(LogFile logFile) {
|
protected void loadDefaults(LogFile logFile) {
|
||||||
LoggerContext context = getLoggerContext();
|
LoggerContext context = getLoggerContext();
|
||||||
context.stop();
|
stopAndReset(context);
|
||||||
context.reset();
|
|
||||||
LogbackConfigurator configurator = new LogbackConfigurator(context);
|
LogbackConfigurator configurator = new LogbackConfigurator(context);
|
||||||
new DefaultLogbackConfiguration(logFile).apply(configurator);
|
new DefaultLogbackConfiguration(logFile).apply(configurator);
|
||||||
}
|
}
|
||||||
@ -112,8 +112,7 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem {
|
|||||||
logFile.applyToSystemProperties();
|
logFile.applyToSystemProperties();
|
||||||
}
|
}
|
||||||
LoggerContext context = getLoggerContext();
|
LoggerContext context = getLoggerContext();
|
||||||
context.stop();
|
stopAndReset(context);
|
||||||
context.reset();
|
|
||||||
try {
|
try {
|
||||||
URL url = ResourceUtils.getURL(location);
|
URL url = ResourceUtils.getURL(location);
|
||||||
new ContextInitializer(context).configureByResource(url);
|
new ContextInitializer(context).configureByResource(url);
|
||||||
@ -124,6 +123,21 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
@Override
|
||||||
protected void reinitialize() {
|
protected void reinitialize() {
|
||||||
getLoggerContext().reset();
|
getLoggerContext().reset();
|
||||||
|
@ -146,6 +146,18 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
|
|||||||
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
|
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
|
@Test
|
||||||
public void jbossLoggingIsConfiguredToUseSlf4j() {
|
public void jbossLoggingIsConfiguredToUseSlf4j() {
|
||||||
this.loggingSystem.beforeInitialize();
|
this.loggingSystem.beforeInitialize();
|
||||||
|
Loading…
Reference in New Issue
Block a user