-
Notifications
You must be signed in to change notification settings - Fork 8
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
Add support for Log4j 2's ThreadContext #207
Add support for Log4j 2's ThreadContext #207
Conversation
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.
Thanks for your great work!
I did an initial review from the online github interface but will check out your branch to have a closer look from the IDE.
Please don't be overwhelmed by the number of comments; I tried to be constructive in my criticism and I like the general quality of this PR, which again is greatly appreciated!
context-propagation-java5/src/main/java/nl/talsmasoftware/context/ContextSnapshot.java
Outdated
Show resolved
Hide resolved
context-propagation-java5/src/main/java/nl/talsmasoftware/context/clearable/Clearable.java
Outdated
Show resolved
Hide resolved
log4j2-propagation/src/main/java/nl/talsmasoftware/context/log4j2/ThreadContextData.java
Outdated
Show resolved
Hide resolved
* @param data data to apply, may be {@code null} | ||
* @param overwrite whether all existing data should overwritten | ||
*/ | ||
static void applyToCurrentThread(ThreadContextData data, boolean overwrite) { |
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.
Why package protected in a public class?
Can't the whole class be package protected in this case?
Please use an interface for the API if you don't want to expose the whole class; this makes it easier to avoid accessibility errors or misuse in the future.
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.
These methods are only intended for usage by ThreadContextManager
. It might also be possible to have this logic only in ThreadContextManager
, work with the public methods of ThreadContextData
and have its constructor be package-private, but I felt the logic belongs closer to ThreadContextData
than to ThreadContextManager
.
As for why these methods are package-private: Most of them provide internal functionality already provided (indirectly) through ThreadContextManager
or through Log4j 2's ThreadContext. Providing it publicly might be confusing and lead to incorrect usage.
Making the whole class package-private is not an option because it is publicly exposed through ThreadContextManager
and would therefore cause usability issues when someone tries to use ThreadContextManager
.
I don't think making it an interface fits really well because it is not expected that the user implements or subclasses this class. Because the package-private methods are not accessible to the user and are not shown in the javadoc either, I don't think that this is an issue.
Would it be alright for you to leave the class as is? If you want I can add a comment (not part of the documentation) to the class describing the rationale I described here.
* Represents a snapshot of the data of the Log4j 2 {@link ThreadContext} of a | ||
* specific thread at a certain point in the past. | ||
*/ | ||
public class ThreadContextData { |
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.
When multiple context managers are applied it may be useful to know this is Log4j2's data?
Perhaps create an interface Log4j2ThreadContext
with implementation Log4j2ThreadcontextData
?
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.
I am not sure if having an interface is worth it, see #207 (comment). We are not expecting the user to provide their own implementation of that class, are we?
log4j2-propagation/src/main/java/nl/talsmasoftware/context/log4j2/ThreadContextManager.java
Outdated
Show resolved
Hide resolved
log4j2-propagation/src/main/java/nl/talsmasoftware/context/log4j2/package-info.java
Outdated
Show resolved
Hide resolved
...j2-propagation/src/main/resources/META-INF/services/nl.talsmasoftware.context.ContextManager
Outdated
Show resolved
Hide resolved
Just to touch base, I have been rather busy lately with many things on my mind. |
Could you please split this PR into the Log4j2 module only and (optionally) create separate PR's for the other concerns? |
I'll create a PR to rename the MDC module (with a new relocation in place for it) |
Yes, I can do that. Though if you don't mind, it would like to wait for your pull request to be merged first and then rebase this pull request onto |
Sorry for the delay; I have rebased the changes onto #217 (so that should be merged first), and addressed some of your review comments (which I then marked as resolved). |
Do you want to rebase on develop again? I merged #217 just now. |
Signed-off-by: Marcono1234 <[email protected]>
Signed-off-by: Marcono1234 <[email protected]>
I have some issues still, but I can work out those myself after I've merged this. |
Thanks for merging the changes! |
Thanks for contributing them! |
Resolves #188
The code orientates itself to the existing SLF4J MDC one.
Noteworthy:
artifactId
) to make the distinction between Log4j 1 & 2 (which use the term MDC as well) clearerThreadContextManager.clear()
clears the ThreadContext of the current thread (see also discussion in SLF4J MDC context clear() does not clear context #204)ThreadContextManager.getActiveContext().close()
does nothing (similar to SLF4J MDC one)