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

相关推荐
需要8266 小时前
容器化 Java:镜像瘦身与 JVM 容器感知(cgroup)
java·开发语言·jvm
淡海水9 小时前
06-02-源码-增量标记算法实现
算法·unity·c#·gc
淡海水10 小时前
06-01-源码-Incremental GC总览与设计
unity·c#·gc
Pniubi12 小时前
ConcurrentHashMap拆分线程池难点再讲
java·jvm
Irene19912 天前
Flink 学习日志 01:前端转大数据的实战避坑指南
java·jvm·flink
小天源2 天前
从 Nginx 日志到访问分析:轻量化网站监测服务的架构实践
jvm·nginx·网络安全·架构·网络监控
十年Java程序媛2 天前
JVM 内存区域与对象内存布局|new 对象底层流程,对象头 MarkWord 详解
java·jvm·spring boot
用户094248568032 天前
第2章:Windows、WSL2 与 Docker 环境搭建Redis
java·jvm
淡海水2 天前
05-02-源码-Boehm GC标记阶段
算法·unity·c#·游戏引擎·gc
梅梅绵绵冰2 天前
JVM虚拟机
java·开发语言·jvm