Before You Begin
This 10-minute tutorial shows you how to activate Java Virtual
Machine (JVM) logging with the Xlog option. You
can use the Xlog option to either configure or
enable logging with the JVM unified logging framework.
Background
In Java Platform, Standard Edition Development Kit 8 (JDK 8)
and earlier versions, the logging made it difficult to identify
the appropriate command for the required log messages. In JDK 9
and later, JVM uses a simplified Xlog option with
a unified logging mechanism. By defining <selectors>,
<output>, <decorators>,
and <output-options> in the Xlog
option, you can easily configure logging messages, because the
option defines precisely what messages are logged, where the
output is displayed, and what it looks like.
The Xlog option generates textual messages and
attaches some meta information, such as associated level and
tags. The message level corresponds to its details, and the tag
set corresponds to the message or the JVM component, such as
Garbage Collector (GC), compiler, or threads. You can configure
the output according to your logging requirements.
This tutorial uses the Memory.java file to
demonstrate the logging options. This use case helps you to
understand the GC log messages for a specific Xlog
option.
What Do You Need?
Set Up
the Java Environment
-
Check the Java version installed on your machine.
java -version
The output prints the Java version. In this example, the Java version is 11.0.1.
java version "11.0.1" 2018-10-16 LTS Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS) Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
-
Verify the available
Xloglogging option.java –Xlog:help
The output should look similar to this sample.
Verify
the Xlog Logging Option
-
Print the details of GC with the
Xlogoption enabled.java –Xlog:gc
The output should look similar to this sample.
-
At the command prompt, find
Memory.javain thexlog_java_obe.zipfile. - Compile
Memory.java.javac Memory.java
The
Memory.javaapplication creates and allocates objects required for GC. In this example,Memory.javacreates objects, which will be collected by GC. - Run
Memorywithgc.java -Xlog:gc Memory
-
Verify the GC events.
-
Print the
infolevel logs of allgc.java -Xlog:gc* Memory
The output should look similar to this sample. In this command, the asterisk (*) denotes the wildcard tag match, so that
gclogs at theinfolevel are displayed. -
Print the
infolevel logs ofheapandgc.java -Xlog:gc+heap Memory
The output should look similar to this sample. In this command, the plus sign (+) works like an AND operator, so that all
gcandheaplogs are displayed. -
Print the
infoanddebuglevel logs ofphaseandgc.java -Xlog:gc+phases=debug Memory
The output should look similar to this sample.
-
Print the
gcorsafepointlogs.java -Xlog:gc,safepoint Memory
The output should look similar to this sample. In this command, the comma works like an OR operator, so that either
gcorsafepointlogs are displayed. -
Print the
infoanddebuglevelgclogs.java -Xlog:gc=debug Memory
The output should look similar to this sample.
-
Print the
info,debug, andtracelevel logs forgcandheap.java -Xlog:gc+heap=trace Memory
The output should look similar to this sample.
-
Print the
gclogs decorated withuptimeandtid.java -Xlog:gc::uptime,tid
The output should look similar to this sample. The
infolevel logs tagged withgcare displayed tostdout. -
Print the
safepointlogs to a file.java -Xlog:disable -Xlog:safepoint=trace:safepointrace.txt Memory
This command turns off all logging, including
warningsanderrors, and then enables logs tagged withsafepointat thetracelevel. Thesafepointtrace.txtfile is created inside thexlog_java_obefolder, and its content should be similar to this sample.
The output should look similar to this sample. The output prints
information about each GC event. This command logs messages
that are tagged with gc, using the info
level to stdout.
Activate
Java Virtual Machine Logging with the Xlog Option