In java programs, we use logs to locate and discover potential problems in our projects.In modern java projects, we use logging frameworks such as log4j or slf4j, Logback, and so on to handle logging issues.
JVM is the basis of running java programs. Events in JVM such as GC, class loading, JPMS, heap, threads and so on can actually be logged.Through these logs, we can monitor events in the JVM and, in turn, tune java applications.
The Xlog log log service introduced in JDK9 was created for this purpose.
Through xlog, JDK unifies all kinds of events in JVM and outputs them out in a unified form.Subsystems are distinguished by tag parameters, emergency events are distinguished by log level, and output addresses are configured by logging output.
More please visit www.flydean.com
Use of xlog
Let's start with the simplest example of using xlog:
java -Xlog -version
[0.016s][info][os] Use of CLOCK_MONOTONIC is supported [0.016s][info][os] Use of pthread_condattr_setclock is not supported [0.016s][info][os] Relative timed-wait using pthread_cond_timedwait is associated with the default clock [0.017s][info][os] SafePoint Polling address, bad (protected) page:0x0000000108901000, good (unprotected) page:0x0000000108902000 [0.022s][info][biasedlocking] Aligned thread 0x00007f983e008200 to 0x00007f983e008800 [0.023s][info][os,thread ] Thread attached (tid: 10499, pthread id: 123145571979264).
The log is very long, not all of it is listed here.From the output log, we can see that the JVM performs many operations in the java-verson command.
We can see that the log lists the time spent on each operation, the log level, and the classification to which the operation belongs.
Through these logs, we can have a better understanding of how the JVM works.
Using Java -Xlog:helpCommand us to look at the basic format of xlog:
-Xlog Usage: -Xlog[:[selections][:[output][:[decorators][:output-options]]]] where 'selections' are combinations of tags and levels of the form tag1[+tag2...][*][=level][,...] NOTE: Unless wildcard (*) is specified, only log messages tagged with exactly the tags specified will be matched.
selections represent exactly what information needs to be output.Is expressed as tag=level.
Tags represent events or subsystems in the JVM:
Available log tags: add, age, alloc, annotation, aot, arguments, attach, barrier, biasedlocking, blocks, bot, breakpoint, bytecode, cds, census, class, classhisto, cleanup, codecache, compaction, compilation, constantpool, constraints, container, coops, cpu, cset, data, datacreation, dcmd, decoder, defaultmethods, director, dump, dynamic, ergo, event, exceptions, exit, fingerprint, free, freelist, gc, handshake, hashtables, heap, humongous, ihop, iklass, init, inlining, install, interpreter, itables, jfr, jit, jni, jvmti, liveness, load, loader, logging, malloc, mark, marking, membername, memops, metadata, metaspace, methodcomparator, mirror, mmu, module, monitorinflation, monitormismatch, nestmates, nmethod, normalize, numa, objecttagging, obsolete, oldobject, oom, oopmap, oops, oopstorage, os, pagesize, parser, patch, path, perf, periodic, phases, plab, preorder, preview, promotion, protectiondomain, ptrqueue, purge, record, redefine, ref, refine, region, reloc, remset, resolve, safepoint, sampling, scavenge, setting, smr, stackmap, stacktrace, stackwalk, start, startuptime, state, stats, streaming, stringdedup, stringtable, subclass, survivor, sweep, symboltable, system, table, task, thread, time, timer, tlab, tracking, unload, unshareable, update, verification, verify, vmmutex, vmoperation, vmthread, vtables, vtablestubs, workgang Specifying 'all' instead of a tag combination matches all tag combinations
levels represent the level of logging:
Available log levels: off, trace, debug, info, warning, error
Here is an example:
java -Xlog:os,class=info -version
[0.002s][info][os] Use of CLOCK_MONOTONIC is supported [0.002s][info][os] Use of pthread_condattr_setclock is not supported [0.002s][info][os] Relative timed-wait using pthread_cond_timedwait is associated with the default clock [0.003s][info][os] SafePoint Polling address, bad (protected) page:0x0000000109543000, good (unprotected) page:0x0000000109544000 [0.006s][info][os] attempting shared library load of /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home/lib/libjava.dylib [0.007s][info][os] shared library load of /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home/lib/libjava.dylib was successful [0.007s][info][os] attempting shared library load of /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home/lib/libzip.dylib [0.010s][info][os] shared library load of /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home/lib/libzip.dylib was successful
Output indicates where to output the log.
stdout for standard output and stderr for standard error.File means output to a file.
java -Xlog:all=debug:file=debug.log -version
decorators indicate what is output to the log.
time (t), utctime (utc), uptime (u), timemillis (tm), uptimemillis (um), timenanos (tn), uptimenanos (un), hostname (hn), pid (p), tid (ti), level (l), tags (tg) Decorators can also be specified as 'none' for no decoration
Look at this example:
java -Xlog:gc*=debug:stdout:time,uptimemillis,tid -version
[2020-05-05T16:12:06.871-0800][32ms] Heap region size: 1M [2020-05-05T16:12:06.871-0800][32ms] Minimum heap 8388608 Initial heap 134217728 Maximum heap 2147483648 [2020-05-05T16:12:06.872-0800][33ms] Heap address: 0x0000000780000000, size: 2048 MB, Compressed Oops mode: Zero based, Oop shift amount: 3 [2020-05-05T16:12:06.872-0800][33ms] ConcGCThreads: 1 offset 8 [2020-05-05T16:12:06.872-0800][33ms] ParallelGCThreads: 4
xlog is a very useful feature provided in JDK9.You can use it in your daily work.
More interesting and see:
- Block Chain From Getting Started to Abandoning Series of Tutorials - Covering Cryptography, Super Ledger, Etaifang, Libra, Bitcoins, etc.
- Spring Boot 2.X Series Tutorials: Seven Days From Nothing to Mastering Spring Boot - Continuous Updates
- Spring 5.X Series Tutorial: Satisfy All Your Imagination of Spring 5 - Continuous Update
- java programmer's journey from small worker to expert (2020) - Continuous update with detailed article tutorials
Author: Fldean Programs
Links to this article: http://www.flydean.com/jdk9-jvm-xlog/
Source: Fldean's blog
Welcome to my Public Number: program stuff, more exciting waiting for you!