Skip to content

Latest commit

 

History

History
 
 

native-image-logging-examples

Configuring Logging in Native Image

This demo shows how the java.util.logging.* API can be used with Native Image. If your application requires additional logging handlers, you can register them with a custom configuration file, logging.properties , and initialize at build time. This reduces the size of the resulting executable file and improves startup time. Unless your application needs to process logging.properties at run time, this approach is recommended. Both approaches are demonstrated in the examples.

Preparation

  1. Download and install the latest GraalVM JDK with Native Image using the GraalVM JDK Downloader:

    bash <(curl -sL https://get.graalvm.org/jdk) -c 'native-image' 
  2. Download or clone the repository and navigate into the native-image-logging-examples directory:

    git clone https://github.com/graalvm/graalvm-demos
    cd graalvm-demos/native-image-logging-examples

There are two Java classes: one for the build-time logger initialization and the second for runtime logger initialization. The logger will be initialized with a custom logging.properties configuration file, which is placed in the same directory as LoggerBuildTimeInit.java and LoggerRunTimeInit.java.

Initializing a Logger at Build Time

In this example, the logger will be initialized at build time with a custom logging.properties configuration file, placed in the same repository as LoggerBuildTimeInit.java.

  1. Compile LoggerBuildTimeInit.java using javac:

    $JAVA_HOME/bin/javac LoggerBuildTimeInit.java
  2. Build and run the native executable:

    $JAVA_HOME/bin/native-image LoggerBuildTimeInit --initialize-at-build-time=LoggerBuildTimeInit
    ./loggerbuildtimeinit

    It should produce the output that looks similar to:

    WARNING: Danger, Will Robinson! [Wed May 18 17:22:40 BST 2022]

The logging.properties file is processed when the executable is built. LoggerHolder.LOGGER is initialized at build time and is available at runtime, therefore improving the startup time. Unless your application needs to process a custom logging.properties configuration file at runtime, this approach is recommended.

Initializing a Logger at Runtime

The logger can also be initialized at runtime.

  1. Compile LoggerRunTimeInit.java using javac:

    $JAVA_HOME/bin/javac LoggerRunTimeInit.java
  2. Build and run the native executable:

    $JAVA_HOME/bin/native-image LoggerRunTimeInit -H:IncludeResources="logging.properties"
    ./loggerruntimeinit 

    It should produce the output that looks similar to:

    WARNING: Danger, Will Robinson! [Wed May 18 17:22:40 BST 2022]

    In this case, the logging.properties file needs to be available for runtime processing and it must be included in the executable via the -H:IncludeResources=logging.properties option. For more details, see Use of Resources in a Native Executable.

Native Image supports logging using the java.util.logging.* API. The logging configuration by default is based on the logging.properties file found in the JDK.

Related Documentation