Java11使用JVM同一日志框架启用日志记录

你可以使用-Xlog选项配置或启用Java虚拟机同一日志框架的日志记录。

sh 复制代码
-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
指令格式:
sh 复制代码
-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

日志消息用有关消息的信息装饰。您可以将每个输出配置为使用一组自定义的装饰器。输出的顺序始终与如下表中列出的顺序相同。

装饰器 描述
timeort Current time and date in ISO-8601 format.
utctimeorutc Universal Time Coordinated or Coordinated Universal Time.
uptimeoru Time since the start of the JVM in seconds and milliseconds. For example, 6.567s.
timemillisortm The same value as generated by System.currentTimeMillis().
uptimemillisorum Milliseconds since the JVM started.
timenanosortn The same value generated by System.nanoTime().
uptimenanosorun Nanoseconds since the JVM started.
hostnameorhn The host name.
pidorp The process identifier.
tidorti The thread identifier.
levelorl The level associated with the log message.
tagsortg The tag-set associated with the log message.
四、-Xlog使用案例
  • -Xlog 通过使用日志info级别将所有信息记录到stdout输出流,并配置uptime、levels、tags装饰
sh 复制代码
-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

相关推荐
晚安code1 小时前
JVM 类加载机制详解:双亲委派模型的原理、优势与三种打破方式
jvm
Joe_Wang54 小时前
【从0到1学习JVM · 21】既不浪费空间又没有碎片,标记整理算法到底慢在哪
jvm·学习·算法·垃圾回收·标记整理算法
Cheney Pan5 小时前
第4篇 Spring Boot 应用监控:Micrometer 与 JVM 指标
jvm·spring boot·后端·prometheus
淡海水5 小时前
02-01-原理篇-Mark-Sweep与变种算法
算法·unity·c#·游戏引擎·.net·gc
Joe_Wang55 小时前
【从0到1学习JVM · 25】同样都要停顿所有线程,Parallel比Serial到底强在哪
java·jvm·学习·垃圾回收
wuminyu5 小时前
JVM虚拟线程的底层实现原理分析
java·linux·c语言·jvm·c++
wuminyu17 小时前
C++协程实现接收端的零拷贝Buffer管理原理剖析
java·linux·c语言·jvm·c++
波力海苔夹心脆67519 小时前
C# 值类型与引用类型详解:存储位置、赋值机制、参数传递、相等比较、装箱拆箱与常见陷阱
开发语言·jvm·经验分享·笔记·c#·.net
Joe_Wang51 天前
【从0到1学习JVM · 15】对象在堆里来回倒腾,究竟怎样才会进老年代?
java·jvm·学习·垃圾回收
小宋10211 天前
SQL + RAG 混合问答实战:结构化指标与文档证据如何统一路由
java·jvm·人工智能·sql