Skip to content

Support using Log4j2 in a GraalVM native image #37152

Open
@msosa

Description

@msosa

Starting a native image built with bootBuildImage and org.springframework.boot:spring-boot-starter-log4j2 throws an error on startup

Exception in thread "main" java.lang.ExceptionInInitializerError
        at org.apache.logging.slf4j.Log4jMarkerFactory.<clinit>(Log4jMarkerFactory.java:36)
        at org.apache.logging.slf4j.SLF4JServiceProvider.initialize(SLF4JServiceProvider.java:53)
        at org.slf4j.LoggerFactory.bind(LoggerFactory.java:183)
        at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:170)
        at org.slf4j.LoggerFactory.getProvider(LoggerFactory.java:455)
        at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:441)
        at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:390)
        at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:416)
        at com.example.log4j2native.Log4j2NativeApplicationKt.<clinit>(Log4j2NativeApplication.kt:10)
Caused by: java.lang.IllegalStateException: java.lang.InstantiationException: org.apache.logging.log4j.message.DefaultFlowMessageFactory
        at org.apache.logging.log4j.spi.AbstractLogger.createDefaultFlowMessageFactory(AbstractLogger.java:242)
        at org.apache.logging.log4j.spi.AbstractLogger.<init>(AbstractLogger.java:141)
        at org.apache.logging.log4j.status.StatusLogger.<init>(StatusLogger.java:141)
        at org.apache.logging.log4j.status.StatusLogger.<clinit>(StatusLogger.java:91)
        ... 9 more
Caused by: java.lang.InstantiationException: org.apache.logging.log4j.message.DefaultFlowMessageFactory
        at java.base@17.0.7/java.lang.Class.newInstance(DynamicHub.java:639)
        at org.apache.logging.log4j.spi.AbstractLogger.createDefaultFlowMessageFactory(AbstractLogger.java:240)
        ... 12 more
Caused by: java.lang.NoSuchMethodException: org.apache.logging.log4j.message.DefaultFlowMessageFactory.<init>()
        at java.base@17.0.7/java.lang.Class.checkMethod(DynamicHub.java:1040)
        at java.base@17.0.7/java.lang.Class.getConstructor0(DynamicHub.java:1206)
        at java.base@17.0.7/java.lang.Class.newInstance(DynamicHub.java:626)
        ... 13 more

I did try adding some hints to help make it through these errors

hints.reflection().registerType(TypeReference.of("org.apache.logging.log4j.message.DefaultFlowMessageFactory"), MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)
hints.reflection().registerType(TypeReference.of("org.apache.logging.log4j.message.ParameterizedMessageFactory"), MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)

But I couldn't fix the error after these were added.

Here is a repo to help reproduce the error(without the hints): https://github.com/msosa/log4j2-native

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: blockedAn issue that's blocked on an external project changetype: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions