-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[LOG4J2-3634] move invocation of propagateLogLevels() #1140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LOG4J2-3634] move invocation of propagateLogLevels() #1140
Conversation
in order to wait for the handlers to be registered in the root logger
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank You for your contribution. Except two small synchronization issues, it looks good to me.
While this is an issue specific to Tomcat, which fails to properly configure the root logger if Logger.getLogger("")
is called by any handler constructor, I am afraid that many other LogManager
implementations may suffer from this bug. Therefore you solution is the cleanest we can get.
log4j-jul/src/main/java/org/apache/logging/log4j/jul/Log4jBridgeHandler.java
Outdated
Show resolved
Hide resolved
log4j-jul/src/main/java/org/apache/logging/log4j/jul/Log4jBridgeHandler.java
Outdated
Show resolved
Hide resolved
Thanks for your reply and feedback, @ppkarwasz |
@ppkarwasz It seems to me, the code format wasn't correct. I re-ran spotless and committed the change. Sorry for the frustration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mknet, thanks.
I'll leave this PR open for a couple of days to give a chance to other maintainers to review it and then I'll merge it.
@SuppressWarnings("resource") // no need to close the AutoCloseable ctx here | ||
LoggerContext context = LoggerContext.getContext(false); | ||
context.addPropertyChangeListener(this); | ||
propagateLogLevels(context.getConfiguration()); | ||
// note: java.util.logging.LogManager.addPropertyChangeListener() could also | ||
// be set here, but a call of JUL.readConfiguration() will be done on purpose | ||
this.installAsLevelPropagator = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By looking at some established practices for lazy initialization (e.g. Commons Lang), there should be a second if (this.installAsLevelPropagator)
inside the synchronized block to ensure that propagateLogLevels
is called only once.
In the master
branch we do have a generic LazyUtil, but it is probably not worth backporting it for a single callsite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It depends if other lazy initialized code could be refactored in this branch, too. Can look for volatile fields for starters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
installAsLevelPropagator
must also be volatile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops I thought volatile
, but I wrote transient
. Thanks, Carter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uhhhh, now it makes sense to me as well. I will modify the code tomorrow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ppkarwasz @carterkozak volatile
is applied. Anything else? Thx!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mknet: a second check for installAsLevelPropagator
is needed so that if two thread enter in the first conditional block one will update the levels and the other will wait until it is done:
if (this.installAsLevelPropagator) {
synchronized(this) {
if (this.installAsLevelPropagator) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check. Will come back to you in the next couple of hours.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ppkarwasz Back again and committed the change including some describing comments.
As for me, I would like to clean-up the code at other lines as well. But first things first.
Thank you for finding and fixing this bug. |
@ppkarwasz It was a pleasure! Thanks for your support. |
See https://issues.apache.org/jira/browse/LOG4J2-3634