This documentation may be out of date. See the latest version.

Logging in Native Image

Out of the box, Native Image supports logging using the java.util.logging.* API.

Default Logging Configuration #

The logging configuration built in a native image by default is based on the file found in the JDK. This configures a java.util.logging.ConsoleHandler which will only show messages at the INFO and above levels. Custom logging configuration can be loaded either at image build time or at run time as described below.

Note that if additional logging handlers are used, the corresponding classes need to be registered for reflection. For example, if java.util.logging.FileHandler is used then the following reflection configuration is necessary:

    "name" : "java.util.logging.FileHandler",
    "methods" : [
      { "name" : "<init>", "parameterTypes" : [] },

See the Reflection Support page for more details.

Build-Time Logger Initialization #

The logger can be initialized at image build time with a custom config, as in the code below:

public class BuildTimeLoggerInit {
  private static final Logger LOGGER;
  static {
    LOGGER = Logger.getLogger(BuildTimeLoggerInit.class.getName());

  public static void main(String[] args) throws IOException {
    // Use the LOGGER here

The file is processed at image build time. It does not need to be included in the native image, therefore reducing the image size.

LoggerHolder.LOGGER is also initialized at image build time and is readily available at run time, therefore improving the startup. Unless the application needs to process a custom configuration at run time, this approach is recommended.

Runtime Logger Initialization #

The logger can also be initialized at run time, as in the code below:

public class RuntimeLoggerInit {
    public static void main(String[] args) throws IOException {
        Logger logger = Logger.getLogger(RuntimeLoggerInit.class.getName());
        // Use the logger here

In this case, the file needs to be available for runtime processing and it must be included in the image via the option.

See the information about accessing resources at runtime for more details on this option.

Connect with us