JDK9的新特性:JVM的xlog
文章目錄
- 簡(jiǎn)介
- xlog的使用
- selections
- output
- decorators
- 總結(jié)
簡(jiǎn)介
在java程序中,我們通過(guò)日志來(lái)定位和發(fā)現(xiàn)項(xiàng)目中可能出現(xiàn)的問(wèn)題。在現(xiàn)代java項(xiàng)目中,我們使用log4j或者slf4j,Logback等日志記錄框架來(lái)處理日志問(wèn)題。
JVM是java程序運(yùn)行的基礎(chǔ),JVM中各種事件比如:GC,class loading,JPMS,heap,thread等等其實(shí)都可以有日志來(lái)記錄。通過(guò)這些日志,我們可以監(jiān)控JVM中的事件,并可以依次來(lái)對(duì)java應(yīng)用程序進(jìn)行調(diào)優(yōu)。
在JDK9中引入的Xlog日志服務(wù)就是為這個(gè)目的而創(chuàng)建的。
通過(guò)xlog,JDK將JVM中的各種事件統(tǒng)一起來(lái),以統(tǒng)一的形式對(duì)外輸出。通過(guò)tag參數(shù)來(lái)區(qū)分子系統(tǒng),通過(guò)log level來(lái)區(qū)分事件的緊急性,通過(guò)logging output來(lái)配置輸出的地址。
更多內(nèi)容請(qǐng)?jiān)L問(wèn)www.flydean.com
xlog的使用
先看一個(gè)最簡(jiǎn)單的xlog的使用例子:
java -Xlog -version輸出結(jié)果:
[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).日志非常非常長(zhǎng),這里就不全部列出來(lái)了。從輸出的日志我們可以看到j(luò)ava -verson命令中JVM執(zhí)行了諸多的操作。
我們可以看到日志中對(duì)每一個(gè)操作都列出了操作花費(fèi)的時(shí)間,日志級(jí)別和操作所屬的分類(lèi)。
通過(guò)這些日志,我們對(duì)于JVM的運(yùn)行可以有更加深入的理解。
使用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
selections表示的是到底需要輸出哪些信息。是以tag=level來(lái)表示的。
tag表示的是JVM中的事件或者子系統(tǒng):
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, workgangSpecifying 'all' instead of a tag combination matches all tag combinationslevels表示的是日志的級(jí)別:
Available log levels:off, trace, debug, info, warning, error下面舉個(gè)例子:
java -Xlog:os,class=info -version輸出結(jié)果:
[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 successfuloutput
output表示將日志輸出到什么地方。
output的可選項(xiàng):
stdout/stderrfile=<filename>stdout表示標(biāo)準(zhǔn)輸出,stderr表示標(biāo)準(zhǔn)錯(cuò)誤。file表示輸出到文件里面。
舉個(gè)例子:
java -Xlog:all=debug:file=debug.log -versiondecorators
decorators表示輸出哪些內(nèi)容到日志中。
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看下這個(gè)例子:
java -Xlog:gc*=debug:stdout:time,uptimemillis,tid -version輸出結(jié)果:
[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總結(jié)
xlog是JDK9中提供的非常有用的一個(gè)功能。大家可以在日常的工作中使用。
更多精彩內(nèi)容且看:
- 區(qū)塊鏈從入門(mén)到放棄系列教程-涵蓋密碼學(xué),超級(jí)賬本,以太坊,Libra,比特幣等持續(xù)更新
- Spring Boot 2.X系列教程:七天從無(wú)到有掌握Spring Boot-持續(xù)更新
- Spring 5.X系列教程:滿(mǎn)足你對(duì)Spring5的一切想象-持續(xù)更新
- java程序員從小工到專(zhuān)家成神之路(2020版)-持續(xù)更新中,附詳細(xì)文章教程
本文作者:flydean程序那些事
本文鏈接:http://www.flydean.com/jdk9-jvm-xlog/
本文來(lái)源:flydean的博客
歡迎關(guān)注我的公眾號(hào):程序那些事,更多精彩等著您!
超強(qiáng)干貨來(lái)襲 云風(fēng)專(zhuān)訪(fǎng):近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的JDK9的新特性:JVM的xlog的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Apache Maven ToolCha
- 下一篇: 标记接口,注解和注解处理器的前世今生