Skip to content

Commit 86a5516

Browse files
mknetppkarwasz
authored andcommitted
move the the code block to propagate to publish()
in order to wait for the handlers to be registered in the root logger
1 parent 6eed880 commit 86a5516

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

log4j-jul/src/main/java/org/apache/logging/log4j/jul/Log4jBridgeHandler.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public class Log4jBridgeHandler extends java.util.logging.Handler implements Con
9393

9494
private boolean doDebugOutput = false;
9595
private String julSuffixToAppend = null;
96-
private LoggerContext context;
96+
private boolean installAsLevelPropagator = false;
9797

9898
/**
9999
* Adds a new Log4jBridgeHandler instance to JUL's root logger.
@@ -153,14 +153,7 @@ protected void init(boolean debugOutput, String suffixToAppend, boolean propagat
153153
}
154154
this.julSuffixToAppend = suffixToAppend;
155155

156-
this.context = LoggerContext.getContext(false);
157-
158-
if (propagateLevels) {
159-
context.addConfigurationStartedListener(this);
160-
propagateLogLevels(context.getConfiguration());
161-
// note: java.util.logging.LogManager.addPropertyChangeListener() could also
162-
// be set here, but a call of JUL.readConfiguration() will be done on purpose
163-
}
156+
this.installAsLevelPropagator = propagateLevels;
164157

165158
SLOGGER.debug(
166159
"Log4jBridgeHandler init. with: suffix='{}', lvlProp={}, instance={}",
@@ -173,10 +166,7 @@ protected void init(boolean debugOutput, String suffixToAppend, boolean propagat
173166
public void close() {
174167
// cleanup and remove listener and JUL logger references
175168
julLoggerRefs = null;
176-
if (context != null) {
177-
context.removeConfigurationStartedListener(this);
178-
context = null;
179-
}
169+
LoggerContext.getContext(false).removeConfigurationStartedListener(this);
180170
if (doDebugOutput) {
181171
System.out.println("sysout: Log4jBridgeHandler close(): " + this);
182172
}
@@ -188,8 +178,18 @@ public void publish(final LogRecord record) {
188178
return;
189179
}
190180

191-
final org.apache.logging.log4j.Logger log4jLogger = getLog4jLogger(record);
192-
final String msg = julFormatter.formatMessage(record); // use JUL's implementation to get real msg
181+
if (this.installAsLevelPropagator) {
182+
@SuppressWarnings("resource") // no need to close the AutoCloseable ctx here
183+
LoggerContext context = LoggerContext.getContext(false);
184+
context.addConfigurationStartedListener(this);
185+
propagateLogLevels(context.getConfiguration());
186+
// note: java.util.logging.LogManager.addPropertyChangeListener() could also
187+
// be set here, but a call of JUL.readConfiguration() will be done on purpose
188+
this.installAsLevelPropagator = false;
189+
}
190+
191+
org.apache.logging.log4j.Logger log4jLogger = getLog4jLogger(record);
192+
String msg = julFormatter.formatMessage(record); // use JUL's implementation to get real msg
193193
/* log4j allows nulls:
194194
if (msg == null) {
195195
// JUL allows nulls, but other log system may not

0 commit comments

Comments
 (0)