Skip to content

Commit e69a587

Browse files
committed
Merge pull request #3926 from trecloux/gh-3924_propagate-log-level-to-JUL
* gh-3926: Propagates logback log levels to java.util.logging
2 parents e04fb15 + 22e0a50 commit e69a587

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

spring-boot/src/main/java/org/springframework/boot/logging/Slf4JLoggingSystem.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void cleanUp() {
4646

4747
private void configureJdkLoggingBridgeHandler() {
4848
try {
49-
if (bridgeHandlerIsAvailable()) {
49+
if (isBridgeHandlerAvailable()) {
5050
removeJdkLoggingBridgeHandler();
5151
SLF4JBridgeHandler.install();
5252
}
@@ -56,13 +56,13 @@ private void configureJdkLoggingBridgeHandler() {
5656
}
5757
}
5858

59-
private boolean bridgeHandlerIsAvailable() {
59+
protected final boolean isBridgeHandlerAvailable() {
6060
return ClassUtils.isPresent(BRIDGE_HANDLER, getClassLoader());
6161
}
6262

6363
private void removeJdkLoggingBridgeHandler() {
6464
try {
65-
if (bridgeHandlerIsAvailable()) {
65+
if (isBridgeHandlerAvailable()) {
6666
try {
6767
SLF4JBridgeHandler.removeHandlersForRootLogger();
6868
}

spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
import ch.qos.logback.classic.Level;
3939
import ch.qos.logback.classic.LoggerContext;
40+
import ch.qos.logback.classic.jul.LevelChangePropagator;
4041
import ch.qos.logback.classic.turbo.TurboFilter;
4142
import ch.qos.logback.classic.util.ContextInitializer;
4243
import ch.qos.logback.core.spi.FilterReply;
@@ -99,8 +100,7 @@ public void initialize(String configLocation, LogFile logFile) {
99100
@Override
100101
protected void loadDefaults(LogFile logFile) {
101102
LoggerContext context = getLoggerContext();
102-
context.stop();
103-
context.reset();
103+
stopAndReset(context);
104104
LogbackConfigurator configurator = new LogbackConfigurator(context);
105105
new DefaultLogbackConfiguration(logFile).apply(configurator);
106106
}
@@ -112,8 +112,7 @@ protected void loadConfiguration(String location, LogFile logFile) {
112112
logFile.applyToSystemProperties();
113113
}
114114
LoggerContext context = getLoggerContext();
115-
context.stop();
116-
context.reset();
115+
stopAndReset(context);
117116
try {
118117
URL url = ResourceUtils.getURL(location);
119118
new ContextInitializer(context).configureByResource(url);
@@ -124,6 +123,21 @@ protected void loadConfiguration(String location, LogFile logFile) {
124123
}
125124
}
126125

126+
private void stopAndReset(LoggerContext loggerContext) {
127+
loggerContext.stop();
128+
loggerContext.reset();
129+
if (isBridgeHandlerAvailable()) {
130+
addLevelChangePropagator(loggerContext);
131+
}
132+
}
133+
134+
private void addLevelChangePropagator(LoggerContext loggerContext) {
135+
LevelChangePropagator levelChangePropagator = new LevelChangePropagator();
136+
levelChangePropagator.setResetJUL(true);
137+
levelChangePropagator.setContext(loggerContext);
138+
loggerContext.addListener(levelChangePropagator);
139+
}
140+
127141
@Override
128142
protected void reinitialize() {
129143
getLoggerContext().reset();

spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ public void loggingThatUsesJulIsCaptured() {
146146
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
147147
}
148148

149+
@Test
150+
public void loggingLevelIsPropagatedToJulI() {
151+
this.loggingSystem.beforeInitialize();
152+
this.loggingSystem.initialize(null, null);
153+
this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG);
154+
java.util.logging.Logger julLogger = java.util.logging.Logger
155+
.getLogger(getClass().getName());
156+
julLogger.fine("Hello debug world");
157+
String output = this.output.toString().trim();
158+
assertTrue("Wrong output:\n" + output, output.contains("Hello debug world"));
159+
}
160+
149161
@Test
150162
public void jbossLoggingIsConfiguredToUseSlf4j() {
151163
this.loggingSystem.beforeInitialize();

0 commit comments

Comments
 (0)