当前位置: 首页 > news >正文

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:gcPrints 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*=tracePrints the lowest level of gc logging information. The output displays all gc related tags with detailed logging information.
-Xlog:gc+phases=debugPrints different phase level information. This gives detailed level of information logged at debug level.
-Xlog:gc+heap=debugPrints heap usage details before and after gc. This logs messages tagged with the gc and heap at debug level.
-Xlog:safepointPrints details about application concurrent time and application stop time at the same level.
-Xlog:gc+ergo*=tracePrints 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=tracePrints 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

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

装饰器描述
timeortCurrent time and date in ISO-8601 format.
utctimeorutcUniversal Time Coordinated or Coordinated Universal Time.
uptimeoruTime since the start of the JVM in seconds and milliseconds. For example, 6.567s.
timemillisortmThe same value as generated by System.currentTimeMillis().
uptimemillisorumMilliseconds since the JVM started.
timenanosortnThe same value generated by System.nanoTime().
uptimenanosorunNanoseconds since the JVM started.
hostnameorhnThe host name.
pidorpThe process identifier.
tidortiThe thread identifier.
levelorlThe level associated with the log message.
tagsortgThe 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

http://www.lryc.cn/news/496570.html

相关文章:

  • onlyoffice实现文档比对(Beta版)-纯文字比对(非OCR)
  • JS querySelector方法的优点
  • 利用获取商品详情API:item_get可以获取到淘宝商品详情的哪些数据?
  • 【大数据学习 | 面经】Spark 3.x 中的AQE(自适应查询执行)
  • [Vue]Vue-router
  • 【HarmonyOS】鸿蒙应用使用lottie动画
  • 1.使用docker 部署redis Cluster模式 集群3主3从
  • vue基础之8:computed对比watch
  • Luban数据插件的用法
  • 指针(上)
  • 张伟楠动手学强化学习笔记|第一讲(上)
  • python脚本:Word文档批量转PDF格式
  • 性能测试常见面试问题和答案
  • uniapp进阶技巧:如何优雅地封装request实例
  • 实验五、流式视频服务程序mjpg-streamer移植实验
  • (长期更新)《零基础入门 ArcGIS(ArcMap) 》实验三----学校选址与路径规划(超超超详细!!!)
  • L16.【LeetCode笔记】前序遍历
  • 泰州榉之乡全托机构探讨:自闭症并非家庭的 “末日”
  • BiGRU:双向门控循环单元在序列处理中的深度探索
  • 【vue-router】Vue-router如何实现路由懒加载
  • Linux网络编程基础
  • MySQL中的幻读问题
  • AI后端工程师面试题的内容
  • MFC工控项目实例三十五读取数据库数据
  • OpenWrt -制作ubifs文件系统的固件
  • C++ - 继承
  • 华为服务器使用U盘重装系统
  • 网络分层模型( OSI、TCP/IP、五层协议)
  • 前端开发 之 15个页面加载特效上【附完整源码】
  • Spring Boot使用JDK 21虚拟线程