Oracle by Example brandingActivate Java Virtual Machine Logging with the Xlog Option

section 0Before 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?


section 1Set Up the Java Environment

  1. 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)
  2. Verify the available Xlog logging option.

    java –Xlog:help

    The output should look similar to this sample.

section 2Verify the Xlog Logging Option

  1. Print the details of GC with the Xlog option enabled.

    java –Xlog:gc

    The output should look similar to this sample.

  2. At the command prompt, find Memory.java in the xlog_java_obe.zip file.

  3. Compile Memory.java.
    javac Memory.java

    The Memory.java application creates and allocates objects required for GC. In this example, Memory.java creates objects, which will be collected by GC.

  4. Run Memory with gc.
    java -Xlog:gc Memory
  5. Verify the GC events.

  6. 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.

  7. Print the info level logs of all gc.

    java -Xlog:gc* Memory

    The output should look similar to this sample. In this command, the asterisk (*) denotes the wildcard tag match, so that gc logs at the info level are displayed.

  8. Print the info level logs of heap and gc.

    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 gc and heap logs are displayed.

  9. Print the info and debug level logs of phase and gc.

    java -Xlog:gc+phases=debug Memory

    The output should look similar to this sample.

  10. Print the gc or safepoint logs.

    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 gc or safepoint logs are displayed.

  11. Print the info and debug level gc logs.

    java -Xlog:gc=debug Memory

    The output should look similar to this sample.

  12. Print the info, debug, and trace level logs for gc and heap.

    java -Xlog:gc+heap=trace Memory

    The output should look similar to this sample.

  13. Print the gc logs decorated with uptime and tid.

    java -Xlog:gc::uptime,tid

    The output should look similar to this sample. The info level logs tagged with gc are displayed to stdout.

  14. Print the safepoint logs to a file.

    java -Xlog:disable -Xlog:safepoint=trace:safepointrace.txt Memory

    This command turns off all logging, including warnings and errors, and then enables logs tagged with safepoint at the trace level. The safepointtrace.txt file is created inside the xlog_java_obe folder, and its content should be similar to this sample.


more informationWant to Learn More?