在java程序中,咱們經過日誌來定位和發現項目中可能出現的問題。在現代java項目中,咱們使用log4j或者slf4j,Logback等日誌記錄框架來處理日誌問題。java
JVM是java程序運行的基礎,JVM中各類事件好比:GC,class loading,JPMS,heap,thread等等其實均可以有日誌來記錄。經過這些日誌,咱們能夠監控JVM中的事件,並能夠依次來對java應用程序進行調優。程序員
在JDK9中引入的Xlog日誌服務就是爲這個目的而建立的。segmentfault
經過xlog,JDK將JVM中的各類事件統一塊兒來,以統一的形式對外輸出。經過tag參數來區分子系統,經過log level來區分事件的緊急性,經過logging output來配置輸出的地址。框架
更多內容請訪問 www.flydean.com
先看一個最簡單的xlog的使用例子:less
java -Xlog -version
輸出結果:dom
[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).
日誌很是很是長,這裏就不所有列出來了。從輸出的日誌咱們能夠看到java -verson命令中JVM執行了諸多的操做。ssh
咱們能夠看到日誌中對每個操做都列出了操做花費的時間,日誌級別和操做所屬的分類。jvm
經過這些日誌,咱們對於JVM的運行能夠有更加深刻的理解。oop
使用java -Xlog:help命令咱們看一下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表示的是到底須要輸出哪些信息。是以tag=level來表示的。
tag表示的是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表示的是日誌的級別:
Available log levels: off, trace, debug, info, warning, error
下面舉個例子:
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表示將日誌輸出到什麼地方。
output的可選項:
stdout/stderr file=<filename>
stdout表示標準輸出,stderr表示標準錯誤。file表示輸出到文件裏面。
舉個例子:
java -Xlog:all=debug:file=debug.log -version
decorators表示輸出哪些內容到日誌中。
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
看下這個例子:
java -Xlog:gc*=debug:stdout:time,uptimemillis,tid -version
輸出結果:
[2020-05-05T16:12:06.871-0800][32ms][9475] Heap region size: 1M [2020-05-05T16:12:06.871-0800][32ms][9475] Minimum heap 8388608 Initial heap 134217728 Maximum heap 2147483648 [2020-05-05T16:12:06.872-0800][33ms][9475] Heap address: 0x0000000780000000, size: 2048 MB, Compressed Oops mode: Zero based, Oop shift amount: 3 [2020-05-05T16:12:06.872-0800][33ms][9475] ConcGCThreads: 1 offset 8 [2020-05-05T16:12:06.872-0800][33ms][9475] ParallelGCThreads: 4
xlog是JDK9中提供的很是有用的一個功能。你們能夠在平常的工做中使用。
更多精彩內容且看:
本文做者:flydean程序那些事本文連接:http://www.flydean.com/jdk9-jvm-xlog/
本文來源:flydean的博客
歡迎關注個人公衆號:程序那些事,更多精彩等着您!