- Latest
- 22.3
- 22.2
- 22.1
- 22.0
- 21.3
- Dev Build
- Native Image
- Build Output
- Build Configuration
- Tracing Agent
- 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 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 Images
- 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
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 logging.properties
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 logging.properties
config, as in the code below:
public class BuildTimeLoggerInit {
private static final Logger LOGGER;
static {
LogManager.getLogManager().readConfiguration(BuildTimeLoggerInit.class.getResourceAsStream("logging.properties"));
LOGGER = Logger.getLogger(BuildTimeLoggerInit.class.getName());
}
public static void main(String[] args) throws IOException {
// Use the LOGGER here
}
}
The logging.properties
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 logging.properties
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 {
LogManager.getLogManager().readConfiguration(RuntimeLoggerInit.class.getResourceAsStream("logging.properties"));
Logger logger = Logger.getLogger(RuntimeLoggerInit.class.getName());
// Use the logger here
}
}
In this case, the logging.properties
file needs to be available for runtime processing and it must be included in the image via the -H:IncludeResources=logging.properties
option.
See the information about accessing resources at runtime for more details on this option.