beryx / badass-jlink-plugin

Create a custom runtime image of your modular application
https://badass-jlink-plugin.beryx.org
Apache License 2.0
387 stars 27 forks source link

jlink issues with Log4j 2.18.0 / 2.19.0 / 2.20.0 / 2.21.x #217

Closed CodeDead closed 3 weeks ago

CodeDead commented 2 years ago

Opening a new issue because after the fix from this one https://github.com/beryx/badass-jlink-plugin/issues/14

It seems like the problem has returned for log4j 2.19.0

jlink {
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    forceMerge('log4j-api' )
    ...
}

dependencies {
    implementation 'org.apache.logging.log4j:log4j-core:2.19.0'
}

When running the jlinked binary, you will get an error very similar to:

java.util.ServiceConfigurationError: org.apache.logging.log4j.spi.Provider: module com.codedead.merged.module does not declare `uses`
...
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
ERROR StatusLogger Unable to load services for service interface org.apache.logging.log4j.core.util.ContextDataProvider
CodeDead commented 2 years ago

For anyone facing this issue, I've found a work-around by manually adding the following 'uses' statements to your mergedModule block:

jlink {
...
    mergedModule {
        additive = true
        uses 'org.apache.logging.log4j.util.PropertySource';
        uses 'org.apache.logging.log4j.core.util.ContextDataProvider';
        uses 'org.apache.logging.log4j.core.util.WatchEventService';
        uses 'org.apache.logging.log4j.spi.Provider';
        uses 'org.apache.logging.log4j.message.ThreadDumpMessage.ThreadInfoFactory';
    }
}
tbnguyen1407 commented 2 years ago

I filed same issue months ago here

https://github.com/beryx-gist/badass-jlink-example-log4j2-javafx/issues/5

I also manually added uses clauses but it is not working for all cases.

Hopefully it gets fixed soon.

vewert commented 2 years ago

I just ran into this same issue, thanks for logging it, and for posting a work-around, it saved me many hours of head-scratching!

xzel23 commented 1 year ago

According to the log4j 2.21.0 release notes they migrated from automatic modules to named modules. This might or might not solve this problem. Note that the module names for the bridge implementations changed.

CodeDead commented 1 year ago

According to the log4j 2.21.0 release notes they migrated from automatic modules to named modules. This might or might not solve this problem. Note that the module names for the bridge implementations changed.

No luck there unfortunately. There's a new error message in 2.21.0: Module org.apache.logging.log4j.core does not read a module that exports javax.annotation.processing

Going to look for a work-around.

CodeDead commented 1 year ago

As per https://github.com/apache/logging-log4j2/issues/1896 , the solution for log4j2 2.21.0 is to do something like this in your build.gradle file:

jlink {
    forceMerge('log4j-api')

    mergedModule {
        additive = true
        uses 'org.apache.logging.log4j.util.PropertySource'
        uses 'org.apache.logging.log4j.spi.Provider'
        uses 'org.apache.logging.log4j.message.ThreadDumpMessage.ThreadInfoFactory'
    }

You will also need to add the following to your module-info:

    requires java.compiler;
    requires java.naming;
    requires org.apache.logging.log4j;
    requires org.apache.logging.log4j.core;
xzel23 commented 3 weeks ago

Hello @CodeDead, does the problem persist with current log4j versions? There are several fixes contained in the log4j release notes for versions 2..22+. If this has been fixed, please close the issue.

CodeDead commented 3 weeks ago

Hi @xzel23

Thanks for the reminder. I just checked with Log4j2 2.24 and you still need to manually add the workaround discussed in this comment: https://github.com/beryx/badass-jlink-plugin/issues/217#issuecomment-1776917698

E.g.

...
    mergedModule {
        additive = true
        uses 'org.apache.logging.log4j.util.PropertySource'
        uses 'org.apache.logging.log4j.spi.Provider'
        uses 'org.apache.logging.log4j.message.ThreadDumpMessage.ThreadInfoFactory'
    }

Edit: Nevermind, the issue is fixed if you remove the forceMerge and merged Module additives!

Kind regards