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

相关推荐
磁爆步兵13 小时前
内存分区:程序运行的核心秘密
java·开发语言·jvm
讓丄帝愛伱15 小时前
JVM线上故障快速排
jvm
落苜蓿蓝15 小时前
Java 循环中对象复用导致属性覆盖?从 JVM 内存模型讲解原因
java·jvm·python
一只专注api接口开发的技术猿16 小时前
OpenClaw 实战:搭建京东商品监控与简易数据分析脚本
大数据·jvm·数据库·oracle·数据分析
Tim_101 天前
【C++】021、RAII
jvm
格兰芬多呼神护卫1 天前
# Agent 企业评估方案深度分析:从“看答案”到持续质量闭环_码士教育
java·开发语言·jvm
CodeStats2 天前
【Java GC】Java JVM 垃圾回收(GC)完全指南:从内存结构到并发回收的底层原理
java·jvm·gc·垃圾回收·zgc·内存结构
ChaHae-In2 天前
JVM运行时数据区与垃圾回收
jvm
IKUN家族2 天前
Spring JVM(二)
java·jvm·spring
程序员天天困2 天前
Arthas ognl 表达式从入门到实战:掌握在线调试最强的表达式引擎
java·jvm·后端