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

Backport some Log4j API 3.x features #2392

Merged
merged 12 commits into from
Mar 22, 2024
Merged

Backport some Log4j API 3.x features #2392

merged 12 commits into from
Mar 22, 2024

Conversation

ppkarwasz
Copy link
Contributor

@ppkarwasz ppkarwasz commented Mar 19, 2024

We move some features found in the main branch of log4j-api back to 2.x:

  • It adds a ServiceLoaderUtil#safeStream method that takes an instance of ServiceLoader as a parameter. Since now the caller must call ServiceLoader#load, we can remove the previous methods that created lambdas through reflection to workaround the caller-sensitivity of ServiceLoader#load. Needless to say, the new approach is much more GraalVM-friendly,
  • It makes all the ReusableParameterizedMessage#set methods public,
  • It removes the @Deprecated annotation from Supplier and BiConsumer and let them extend the j.u.f interfaces of the same name,
  • It directly instantiates the default MessageFactory and FlowMessageFactory,
  • It directly instantiates the LoggerContextFactory and ThreadContextMap for the log4j-to-jul and log4j-to-slf4j implementations.

After these changes we reach two goals:

@ppkarwasz
Copy link
Contributor Author

As a curiosity, when working with GraalVM and either log4j-to-jul or log4j-to-slf4j (i.e. without Log4j Core) I get this nice warning from GraalVM:

Warning: The log4j library has been detected, but the version is unavailable. Due to Log4Shell, please ensure log4j is at version 2.17.1 or later.

Copy link
Member

@vy vy left a comment

Choose a reason for hiding this comment

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

Really liked the new LoaderUtil#safeStream() usage allowing GraalVM compat! 🤩

Comment on lines +39 to +42
<bnd-extra-package-options>
<!-- Not exported by most OSGi system bundles, hence we use the system classloader to load `sun.reflect.Reflection` -->
!sun.reflect
</bnd-extra-package-options>
Copy link
Member

Choose a reason for hiding this comment

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

Has this problem ever been reported? I feel like we are solving a problem of your C64 that you tinker at nights in your attic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A sun.reflect;resolution:=optional declaration has been in log4j-api since I joined the team.

We must have lost it, when we switched to BND, because it couldn't detect that we are using sun.reflect.Reflect. However after I replaced LoaderUtil.loadClass with Class.forName the entry reappeared and broke our OSGi tests.

Base automatically changed from feature/provider-revamp to 2.x March 21, 2024 21:54
We make `ServiceLoaderUtil` more GraalVM-friendly by replacing the usage
of `MethodHandles.Lookup` with the requirement for the caller to
instantiate the `ServiceLoader` himself.
By removing the reflective instantiation of `LoggerContextFactory` and
`ThreadContextMap`, we make `log4j-to-jul` and `log4j-to-slf4j` more
GraalVM-friendly.
@ppkarwasz ppkarwasz marked this pull request as ready for review March 22, 2024 10:44
*/
public class Resources {
public final class Resources {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This class is used in @ResourceLock JUnit 5 annotations.

Thanks to the DI, most tests in Log4j Core 3.x will not require to access service classes through static methods (yes, even for ThreadContext), but we want to mark those that need to be rewritten.

@ppkarwasz ppkarwasz merged commit 11209a3 into 2.x Mar 22, 2024
9 checks passed
@ppkarwasz ppkarwasz deleted the fix/backport-log4j3-api branch March 22, 2024 14:22
@ppkarwasz
Copy link
Contributor Author

@vy,

I am merging this PR in order to be able to use 2.24.0-SNAPSHOT versions in the PRs concerning the main branch.

Please let me know if you find something else that needs to be corrected.

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.

3 participants