Skip to content

Latest commit

 

History

History
136 lines (106 loc) · 4.42 KB

File metadata and controls

136 lines (106 loc) · 4.42 KB

Apache Log4j Samples: GraalVM native image

This project shows how to generate a native image using GraalVM.

To use it:

  1. Set the JAVA_HOME environment variable to the path of a GraalVM installation,

  2. Set the current working directory to the folder containing this file,

  3. Generate the artifact locally with:

    ../mvnw clean verify -Puse-<backend>

    where <backend> can be:

    simple

    This profile uses SimpleLogger as logging backend.

    jul

    This profile uses java.util.logging as logging backend.

    logback

    This profile uses Logback as logging backend.

    log4j-core

    This profile uses Log4j Core as logging backend with its out-of-the-box reachability metadata. It uses Pattern Layout to format log events.

    log4j-core-jtl

    This profile uses Log4j Core as logging backend with its out-of-the-box reachability metadata. It uses JSON Template Layout to format log events.

log4j-core-minimal

This profile uses Log4j Core a logging backend with an external reachability metadata source optimized for size. It uses Pattern Layout to format log events.

log4j-core-jtl-minimal

This profile uses Log4j Core a logging backend with an external reachability metadata source optimized for size. It uses JSON Template Layout to format log events.

  1. Run the application with

    target/log4j-samples-graalvm

Reachability metadata

Log4j, as many other Java libraries, relies heavily on reflection to convert the configuration file into an instance of a Configuration object. The classes that are accessed through reflection are called Log4j plugins and GraalVM must be aware of their existence through the usage of reachability metadata.

See Log4j plugins and GraalVM reachability metadata for more details.

The Log4j plugin descriptors and GraalVM reachability metadata used in the customized use-log4j-core-minimal and use-log4j-core-jtl-minimal profiles were generated using the procedure described in the documentation of the log4j-converter-plugin-descriptor tool.

Native binary size

Using GraalVM 21.0.4 on Linux x64, the native binaries have the following size:

Caution

Since this is a trivial Java application, the difference between the binary size without and with Log4j is large: when Log4j is added GraalVM must compile many additional java.base classes.

In a typical application, those classes are already required, so the difference should be smaller.

Configuration Size

With SimpleLogger

13.90 MiB

With java.util.logging

14.10 MiB

With Logback

30.62 MiB

With Log4j Core (manually optimized reachability metadata)

36.19 MiB

With Log4j Core + JSON Template Layout (manually optimized reachability metadata)

37.40 MiB

With Log4j Core (embedded full reachability metadata)

46.80 MiB

With Log4j Core + JSON Template Layout (embedded full reachability metadata)

47.37 MiB