Java11使用JVM同一日志框架启用日志记录
你可以使用-Xlog选项配置或启用Java虚拟机同一日志框架的日志记录。
-Xlog:gc*=trace:file=/Users/xx/gc-%t.log:time,tags,level,pid,tid,hostname,path:filecount=3,filesize=10K
-Xlog:gc*=trace:stdout:time,tags,level,pid,tid,hostname:filecount=3,filesize=10K
-Xlog:gc*=trace:stderr:time,tags,level,pid,tid,hostname:filecount=3,filesize=10K
指令格式:
-Xlog[:[what][:[output][:[decorators][:output-options[,...]]]]]
一、what
指定组合的标记和日志级别,格式为tag1[+tag2…][*][=level][, …]。除非制定了通配符(*),否则只有标记了指定标签的日志消息才会匹配。
-Xlog标记集和日志级别
每条日志消息都有一个级别和一个与之关联的标记集。消息的级别对应于它的详细信息,标记集对应于消息包含的内容或它涉及的JVM组件(如:GC、编译器或线程)。
可用日志级别:
- off
- trace
- debug
- info
- warning
- error
可用的日志标记。指定all而不是标记组合将匹配所有标记组合:
- add
- age
- alloc
- annotation
- aot
- arguments
- attach
- barrier
- biasedlocking
- blocks
- bot
- breakpoint
- bytecode
- census
- class
- classhisto
- cleanup
- compaction
- comparator
- constraints
- constantpool
- coops
- cpu
- cset
- data
- defaultmethods
- dump
- ergo
- event
- exceptions
- exit
- fingerprint
- freelist
- gc
- hashtables
- heap
- humongous
- ihop
- iklass
- init
- itables
- jfr
- jni
- jvmti
- liveness
- load
- loader
- logging
- mark
- marking
- metadata
- metaspace
- method
- mmu
- modules
- monitorinflation
- monitormismatch
- nmethod
- normalize
- objecttagging
- obsolete
- oopmap
- os
- pagesize
- parser
- patch
- path
- phases
- plab
- preorder
- promotion
- protectiondomain
- purge
- redefine
- ref
- refine
- region
- remset
- resolve
- safepoint
- scavenge
- scrub
- setting
- stackmap
- stacktrace
- stackwalk
- start
- startuptime
- state
- stats
- stringdedup
- stringtable
- subclass
- survivor
- sweep
- system
- task
- thread
- time
- timer
- tlab
- unload
- update
- verification
- verify
- vmoperation
- vtables
- workgang
下表描述了标签和日志级别的可能组合列表:
日志标签 | 描述 |
---|---|
-Xlog:gc | Prints the gc information along with time at which the garbage collection occurred. |
-Xlog:gc* | Prints log messages that include at least gc tag. It can also have other tags associated with it. However, it will not give phase level information. |
-Xlog:gc*=trace | Prints the lowest level of gc logging information. The output displays all gc related tags with detailed logging information. |
-Xlog:gc+phases=debug | Prints different phase level information. This gives detailed level of information logged at debug level. |
-Xlog:gc+heap=debug | Prints heap usage details before and after gc . This logs messages tagged with the gc and heap at debug level. |
-Xlog:safepoint | Prints details about application concurrent time and application stop time at the same level. |
-Xlog:gc+ergo*=trace | Prints combination of both gc and ergo messages at trace level. The information includes all details about heap sizing and collection set construction. |
-Xlog:gc+age=trace | Prints the survivor size and age distribution of surviving objects in the survivor spaces at trace level. |
-Xlog:gc*:file=::filecount=,filesize= | Redirects the output to the file, specifies the number of files you want to use and the size of the file in kb . |
二、-Xlog Output
-Xlog选项支持如下三种类型的输出:
- stdout 发送标准输出流到控制台上
- stderr 发送标准错误输出流到控制台上
- file=filename 发送输出到指定的文件
当使用file=filename时,文件名可以指定%p或者%t扩展进程id或者服务启动时的时间,你还可以配置文本文件,以根据文件大小和要归档的文件数量来处理归档。例如,要每10MB归档一次日志文件并保持5个归档文件,指定选项filesize=10M, filecount=5,文件的目标大小不能保证是精确的,它只是一个近似值。文件会归档,最多可以归档5个目标大小为10MB的文件
三、Decorations
日志消息用有关消息的信息装饰。您可以将每个输出配置为使用一组自定义的装饰器。输出的顺序始终与如下表中列出的顺序相同。
装饰器 | 描述 |
---|---|
timeor t | Current time and date in ISO-8601 format. |
utctimeor utc | Universal Time Coordinated or Coordinated Universal Time. |
uptimeor u | Time since the start of the JVM in seconds and milliseconds. For example, 6.567s. |
timemillisor tm | The same value as generated by System.currentTimeMillis() . |
uptimemillisor um | Milliseconds since the JVM started. |
timenanosor tn | The same value generated by System.nanoTime() . |
uptimenanosor un | Nanoseconds since the JVM started. |
hostnameor hn | The host name. |
pidor p | The process identifier. |
tidor ti | The thread identifier. |
levelor l | The level associated with the log message. |
tagsor tg | The tag-set associated with the log message. |
四、-Xlog使用案例
- -Xlog 通过使用日志info级别将所有信息记录到stdout输出流,并配置uptime、levels、tags装饰
-Xlog:all=info:stdout:uptime,levels,tags
- -Xlog:gc 使用info级别日志将带有gc标记的消息记录到stdout输出流,对于所有其它级别为warning的消息,默认配置已生效;
参考文档:https://docs.oracle.com/en/java/javase/11/tools/java.html#GUID-BE93ABDC-999C-4CB5-A88B-1994AAAC74D5__TAGSANDLEVELS-A7A4A0DF
开源SDK:https://github.com/mingyang66/spring-parent