Skip to content

[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

Merged

Conversation

mknet
Copy link

@mknet mknet commented Nov 14, 2022

in order to wait for the handlers to be registered in the root logger
@mknet mknet changed the title LOG4J2-3634 move invocation of propagateLogLevels() [LOG4J2-3634] move invocation of propagateLogLevels() Nov 14, 2022
Copy link
Contributor

@ppkarwasz ppkarwasz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mknet,

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.

@mknet mknet requested a review from ppkarwasz November 15, 2022 10:06
@mknet
Copy link
Author

mknet commented Nov 15, 2022

@mknet,

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.

Thanks for your reply and feedback, @ppkarwasz

@mknet
Copy link
Author

mknet commented Nov 15, 2022

@ppkarwasz It seems to me, the code format wasn't correct. I re-ran spotless and committed the change. Sorry for the frustration.

Copy link
Contributor

@ppkarwasz ppkarwasz left a 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.

Comment on lines 180 to 186
@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;
Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Contributor

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

Copy link
Contributor

@ppkarwasz ppkarwasz Nov 16, 2022

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.

Copy link
Author

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

Copy link
Author

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!

Copy link
Contributor

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) {

Copy link
Author

@mknet mknet Nov 18, 2022

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.

Copy link
Author

@mknet mknet Nov 18, 2022

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.

@ppkarwasz ppkarwasz merged commit 96c2806 into apache:release-2.x Nov 18, 2022
@ppkarwasz
Copy link
Contributor

@mknet,

Thank you for finding and fixing this bug.

@mknet
Copy link
Author

mknet commented Nov 18, 2022

@ppkarwasz It was a pleasure! Thanks for your support.

ppkarwasz pushed a commit to ppkarwasz/logging-log4j2 that referenced this pull request Jan 4, 2024
ppkarwasz pushed a commit to ppkarwasz/logging-log4j2 that referenced this pull request Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants