Skip to content
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

Mixing ContextDataProvider and ThreadContext #2099

Open
slinkydeveloper opened this issue Dec 15, 2023 · 2 comments
Open

Mixing ContextDataProvider and ThreadContext #2099

slinkydeveloper opened this issue Dec 15, 2023 · 2 comments

Comments

@slinkydeveloper
Copy link

Description

Hi all, I just stumbled on an interesting behavior mixing ContextDataProvider and ThreadContext.

In short, my application is using a dependency that provides a custom ContextDataProvider through SPI, but in my application code I also use ThreadContext.

When bundling using the gradle shadowJar plugin, I didn't configured the plugin to merge the SPI files, thus only the custom ContextDataProvider was loaded in the uber JAR, and not the built-in ThreadContextDataProvider.

Although it makes sense from the ServiceLoader POV, I wonder if something can be done to make this behavior less subtle, some ideas:

  • log4j2 could still mount the ThreadContextDataProvider, even though is not registered through SPI
  • log4j2 could print a warning when using ThreadContext but no ThreadContextDataProvider was loaded
  • Perhaps adding some documentation here helps as well https://logging.apache.org/log4j/2.x/manual/extending.html#custom-contextdataprovider , although for the final users that are mixing libraries, this might not be obvious anyway

Configuration

Version: 2.22.0

Operating system: Linux

JDK: 21

@vy
Copy link
Member

vy commented Dec 18, 2023

Shadowing always comes with caveats. If we fix/warn/document this for, say, ThreadContextDataProvider, we should ideally repeat the same for dozens of other Log4j ServiceProviders too. What I would appreciate is a section to the extending page warning users against caveats of shadowing and guiding them how to safely collapse ServiceProviders and Log4j2Plugins.dat files using log4j-transformer. We should ideally share concrete examples for the market leaders (Gradle's shadowJar, flatten-maven-plugin, etc.) too. @slinkydeveloper, is this something you can contribute in a PR?

@ppkarwasz
Copy link
Contributor

I would even make a separate page in the Developer's Guide for shading.

Shading has a lot of aspects, including a legal one. I would include a section that references some answers from LEGAL, e.g.:

[Although I am not entirely satisfied by those answers, since they are prepended by IANAL]

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

No branches or pull requests

3 participants