GraalVM native-image build failure using SLF4J and Log4j 2
See original GitHub issueExpected Behavior
The native-image command should build the image successfully.
Actual Behaviour
The native-image command fails trying to initialize org.apache.logging.slf4j.Log4jLogger
at build time.
Is there a way to use log4j2 with native-image?
I also tried using --initialize-at-build-time=org.apache.logging.slf4j.Log4jLogger
but then another list of classes appears.
Output:
➜ micronaut-slf4j-log4j2 git:(master) native-image -jar build/libs/micronaut-slf4j-log4j2-0.1-all.jar --trace-class-initialization=org.apache.logging.slf4j.Log4jLogger
[micronaut-slf4j-log4j2-native-image:65072] classlist: 2,661.67 ms, 1.19 GB
[micronaut-slf4j-log4j2-native-image:65072] (cap): 411.13 ms, 1.19 GB
WARNING: Method com.sun.management.OperatingSystemMXBean.getCpuLoad() not found.
WARNING: Method com.sun.management.OperatingSystemMXBean.getCpuLoad() not found.
[micronaut-slf4j-log4j2-native-image:65072] setup: 2,010.65 ms, 1.19 GB
WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance.
[micronaut-slf4j-log4j2-native-image:65072] analysis: 7,738.15 ms, 1.73 GB
Error: Classes that should be initialized at run time got initialized during image building:
org.apache.logging.slf4j.Log4jLogger was unintentionally initialized at build time. io.micronaut.runtime.Micronaut caused initialization of this class with the following trace:
at org.apache.logging.slf4j.Log4jLogger.<clinit>(Log4jLogger.java:41)
at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:39)
at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:30)
at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:53)
at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:30)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
at io.micronaut.runtime.Micronaut.<clinit>(Micronaut.java:49)
Error: Use -H:+ReportExceptionStackTraces to print stacktrace of underlying exception
[micronaut-slf4j-log4j2-native-image:65072] [total]: 12,597.98 ms, 1.73 GB
# Printing build artifacts to: /home/brunolellis/workspaces/micronaut-slf4j-log4j2/micronaut-slf4j-log4j2-native-image.build_artifacts.txt
Error: Image build request failed with exit status 1
Steps To Reproduce
sdk use java 21.2.0.r11-grl
./gradlew clean build
native-image -jar build/libs/micronaut-slf4j-log4j2-0.1-all.jar
Environment Information
- Ubuntu 20.04: Linux brunolellis-T490 5.11.0-25-generic #27~20.04.1-Ubuntu SMP Tue Jul 13 17:41:23 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
- ghcr.io/graalvm/graalvm-ce:java11-21
- graalvm: 21.2.0.r11-grl
Example Application
https://github.com/brunolellis/micronaut-slf4j-log4j2
Version
2.5.12
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Error generating native image with Gluonfx for log4j and slf4j ...
I'm trying to build a native image of a JavaFX app with Gluonfx, Micronaut and Graalvm. As logging framework I'm using log4j2 and ......
Read more >Logging in Native Image - GraalVM
This configures a java.util.logging.ConsoleHandler which will only show messages at the INFO level and above. Custom logging configuration can be loaded either ...
Read more >graalvm/native-image - Gitter
im trying to use native-image to build a micronaut application, and I am getting this error $ native-image --no-server -cp myapp.jar [wayaplatform:23757] ...
Read more >@anentropic@fosstodon.org on Twitter: "Got excited when my ...
Got excited when my GraalVM native-image actually compiled, ... Cannot build native image for trivial app with SLF4J and Log4J2 · Issue ...
Read more >Cannot build native image for trivial app with SLF4J and Log4J2
Cannot build native image for trivial app with SLF4J and Log4J2. graalvm. 19 December 2019 Posted by mipastgt. For this trivial application
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Unfortunately, it’s log4j incompatibility with a native image, the issue should be reported to the log4j team. The workaround is to use
log4j-over-slf4j
. Nothing much we can do here.I guess alternatively to getting rid of the Log4j(2) implementation this way (in favor of whatever implementation you use with the SLF4J API, it’s logback in the above example), you could also stick with
log4j-api
(e.g. because you’re using it implicitly vialog4j-api-kotlin
) and take log4j-to-slf4j into use which “forwards” Log4j(2) API calls to the SLF4J API.