- Latest (GraalVM for JDK 21)
- Dev Build
- GraalVM for JDK 21
- GraalVM for JDK 20
- GraalVM for JDK 17
- GraalVM 22.3
- GraalVM 22.2
- GraalVM 22.1
- GraalVM 22.0
- GraalVM 21.3
- Native Image
- Build Output
- Build Configuration
- Tracing Agent
- Experimental Agent Options
- Native Image Compatibility and Optimization Guide
- Class Initialization in Native Image
- Static Native Images
- Native Image Options
- Native Image Hosted and Runtime Options
- Native Image Inspection Tool
- Native Image C API
- Implementing Native Methods in Java with Native Image
- LLVM Backend for Native Image
- Debug Info Feature
- Points-to Analysis Reports
- Using System Properties in Native Image
- Profile-Guided Optimizations
- Memory Management at Image Run Time
- Generating Heap Dumps from Native Images
- JDK Flight Recorder with Native Image
- JCA Security Services on Native Image
- Dynamic Proxy on Native Image
- Java Native Interface (JNI) on Native Image
- Reflection on Native Image
- Accessing Resources in Native Images
- Logging on Native Image
- URL Protocols on Native Image
- Native Image ARM64 Support
- GraalVM Updater
- Languages References
- Embedding Reference
- Polyglot Programming
Note
This documentation may be out of date. See the latest version.
Logging in Native Image
By default, Native Image supports logging via the java.util.logging.*
API.
Default Logging Configuration #
The default logging configuration in a native executable is based on the logging.properties
file found in the JDK.
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 at executable build time or at runtime as described below.
Note that if additional logging handlers are used, the corresponding classes must 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" : [] },
]
}
For more details, see Reflection Support.
Build-Time Logger Initialization #
The logger can be initialized at executable build time with a custom logging.properties configuration file, as illustrated in following example.
- Save the following Java code into a file named BuildTimeLoggerInit.java, then compile it using
javac
:import java.io.IOException; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; public class BuildTimeLoggerInit { private static final Logger LOGGER; static { try { LogManager.getLogManager().readConfiguration(BuildTimeLoggerInit.class.getResourceAsStream("/logging.properties")); } catch (IOException | SecurityException | ExceptionInInitializerError ex) { Logger.getLogger(BuildTimeLoggerInit.class.getName()).log(Level.SEVERE, "Failed to read logging.properties file", ex); } LOGGER = Logger.getLogger(BuildTimeLoggerInit.class.getName()); } public static void main(String[] args) throws IOException { LOGGER.log(Level.WARNING, "Danger, Will Robinson!"); } }
-
Download the logging.properties resource file and save it in the same directory as BuildTimeLoggerInit.java.
-
Build and run the native executable
native-image BuildTimeLoggerInit --initialize-at-build-time=BuildTimeLoggerInit ./buildtimeloggerinit
The logging.properties file is processed at build time. It does not need to be included in the native executable, therefore reducing the size of the executable file.
LoggerHolder.LOGGER
is also initialized at build time and is readily 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.
Runtime Logger Initialization #
The logger can also be initialized at runtime, as shown in the following example.
-
Save the following Java code into a file named RuntimeLoggerInit.java, then compile it using
javac
:import java.io.IOException; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; public class RuntimeLoggerInit { public static void main(String[] args) throws IOException { LogManager.getLogManager().readConfiguration(RuntimeLoggerInit.class.getResourceAsStream("/logging.properties")); Logger logger = Logger.getLogger(RuntimeLoggerInit.class.getName()); logger.log(Level.WARNING, "Danger, Will Robinson!"); } }
-
Download the logging.properties resource file and save it in the same directory as RuntimeLoggerInit.java.
-
Build and run the native executable
native-image RuntimeLoggerInit -H:IncludeResources="logging.properties" ./runtimeloggerinit
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 on this option, see accessing resources at runtime.