简介

在java程序中,我们通过日志来定位和发现项目中可能出现的问题。在现代java项目中,我们使用log4j或者slf4j,Logback等日志记录框架来处理日志问题。

JVM是java程序运行的基础,JVM中各种事件比如:GC,class loading,JPMS,heap,thread等等其实都可以有日志来记录。通过这些日志,我们可以监控JVM中的事件,并可以依次来对java应用程序进行调优。

在JDK9中引入的Xlog日志服务就是为这个目的而创建的。

通过xlog,JDK将JVM中的各种事件统一起来,以统一的形式对外输出。通过tag参数来区分子系统,通过log level来区分事件的紧急性,通过logging output来配置输出的地址。

更多内容请访问www.flydean.com

xlog的使用

先看一个最简单的xlog的使用例子:

java -Xlog -version

输出结果:

[0.016s][info][os] Use of CLOCK_MONOTONIC is supported
[0.016s][info][os] Use of pthread_condattr_setclock is not supported
[0.016s][info][os] Relative timed-wait using pthread_cond_timedwait is associated with the default clock
[0.017s][info][os] SafePoint Polling address, bad (protected) page:0x0000000108901000, good (unprotected) page:0x0000000108902000
[0.022s][info][biasedlocking] Aligned thread 0x00007f983e008200 to 0x00007f983e008800
[0.023s][info][os,thread ] Thread attached (tid: 10499, pthread id: 123145571979264).

日志非常非常长,这里就不全部列出来了。从输出的日志我们可以看到java -verson命令中JVM执行了诸多的操作。

我们可以看到日志中对每一个操作都列出了操作花费的时间,日志级别和操作所属的分类。

通过这些日志,我们对于JVM的运行可以有更加深入的理解。

使用java -Xlog:help命令我们看一下xlog的基本格式:

-Xlog Usage: -Xlog[:[selections][:[output][:[decorators][:output-options]]]]
where 'selections' are combinations of tags and levels of the form tag1[+tag2...][*][=level][,...]
NOTE: Unless wildcard (*) is specified, only log messages tagged with exactly the tags specified will be matched.

selections

selections表示的是到底需要输出哪些信息。是以tag=level来表示的。

tag表示的是JVM中的事件或者子系统:

Available log tags:
add, age, alloc, annotation, aot, arguments, attach, barrier, biasedlocking, blocks, bot, breakpoint, bytecode, cds, census, class, classhisto, cleanup, codecache, compaction, compilation, constantpool, constraints, container, coops, cpu, cset, data, datacreation, dcmd, decoder, defaultmethods, director, dump, dynamic, ergo, event, exceptions, exit, fingerprint, free, freelist, gc, handshake, hashtables, heap, humongous, ihop, iklass, init, inlining, install, interpreter, itables, jfr, jit, jni, jvmti, liveness, load, loader, logging, malloc, mark, marking, membername, memops, metadata, metaspace, methodcomparator, mirror, mmu, module, monitorinflation, monitormismatch, nestmates, nmethod, normalize, numa, objecttagging, obsolete, oldobject, oom, oopmap, oops, oopstorage, os, pagesize, parser, patch, path, perf, periodic, phases, plab, preorder, preview, promotion, protectiondomain, ptrqueue, purge, record, redefine, ref, refine, region, reloc, remset, resolve, safepoint, sampling, scavenge, setting, smr, stackmap, stacktrace, stackwalk, start, startuptime, state, stats, streaming, stringdedup, stringtable, subclass, survivor, sweep, symboltable, system, table, task, thread, time, timer, tlab, tracking, unload, unshareable, update, verification, verify, vmmutex, vmoperation, vmthread, vtables, vtablestubs, workgang
Specifying 'all' instead of a tag combination matches all tag combinations

levels表示的是日志的级别:

Available log levels:
off, trace, debug, info, warning, error

下面举个例子:

java -Xlog:os,class=info -version

输出结果:

[0.002s][info][os] Use of CLOCK_MONOTONIC is supported
[0.002s][info][os] Use of pthread_condattr_setclock is not supported
[0.002s][info][os] Relative timed-wait using pthread_cond_timedwait is associated with the default clock
[0.003s][info][os] SafePoint Polling address, bad (protected) page:0x0000000109543000, good (unprotected) page:0x0000000109544000
[0.006s][info][os] attempting shared library load of /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home/lib/libjava.dylib
[0.007s][info][os] shared library load of /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home/lib/libjava.dylib was successful
[0.007s][info][os] attempting shared library load of /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home/lib/libzip.dylib
[0.010s][info][os] shared library load of /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home/lib/libzip.dylib was successful

output

output表示将日志输出到什么地方。

output的可选项:

 stdout/stderr
file=<filename>

stdout表示标准输出,stderr表示标准错误。file表示输出到文件里面。

举个例子:

java -Xlog:all=debug:file=debug.log -version

decorators

decorators表示输出哪些内容到日志中。

time (t), utctime (utc), uptime (u), timemillis (tm), uptimemillis (um), timenanos (tn), uptimenanos (un), hostname (hn), pid (p), tid (ti), level (l), tags (tg)
Decorators can also be specified as 'none' for no decoration

看下这个例子:

 java -Xlog:gc*=debug:stdout:time,uptimemillis,tid -version

输出结果:

 [2020-05-05T16:12:06.871-0800][32ms][9475] Heap region size: 1M
[2020-05-05T16:12:06.871-0800][32ms][9475] Minimum heap 8388608 Initial heap 134217728 Maximum heap 2147483648
[2020-05-05T16:12:06.872-0800][33ms][9475] Heap address: 0x0000000780000000, size: 2048 MB, Compressed Oops mode: Zero based, Oop shift amount: 3
[2020-05-05T16:12:06.872-0800][33ms][9475] ConcGCThreads: 1 offset 8
[2020-05-05T16:12:06.872-0800][33ms][9475] ParallelGCThreads: 4

总结

xlog是JDK9中提供的非常有用的一个功能。大家可以在日常的工作中使用。

更多精彩内容且看:

本文作者:flydean程序那些事

本文链接:http://www.flydean.com/jdk9-jvm-xlog/

本文来源:flydean的博客

欢迎关注我的公众号:程序那些事,更多精彩等着您!

JDK9的新特性:JVM的xlog的更多相关文章

  1. Jdk14 都要出了,Jdk9 的新特性还不了解一下?

    Java 9 中最大的亮点是 Java 平台模块化的引入,以及模块化 JDK.但是 Java 9 还有很多其他新功能,这篇文字会将重点介绍开发人员特别感兴趣的几种功能. 这篇文章也是 Java 新特性 ...

  2. Java12新特性 -- JVM 常量 API

    Java 12 中引入 JVM 常量 API,用来更容易地对关键类文件 (key class-file) 和运行时构件(artefact)的名义描述 (nominal description) 进行建 ...

  3. 最通俗易懂的 Java 11 新特性讲解

    大多数开发者还是沉浸在 Java 8 中,而 Java 14 将要在 2020 年 3 月 17 日发布了,而我还在写着 Java 11 的新特性.Java 11 是 Java 8 之后的第一个 LT ...

  4. Java8新特性【转】

    地址:http://ifeve.com/java-8-features-tutorial/ 1.简介 毫无疑问,Java 8是自Java  5(2004年)发布以来Java语言最大的一次版本升级,Ja ...

  5. 聊聊 Java8 以后各个版本的新特性

    作者:ZY5A59 juejin.im/post/5d5950806fb9a06b0a277412 某天在网上闲逛,突然看到有篇介绍 Java 11 新特性的文章,顿时心里一惊,毕竟我对于 Java ...

  6. JDK9 新特性

    JDK9 新特性目录导航 目录结构 模块化系统 jshell 多版本兼容JAR 接口的私有方法 改进try-with-resourcs 改进砖石操作符 限制使用单独下划线标识符 String存储结构变 ...

  7. 一文带你看遍 JDK9~14 的重要新特性!

    Java9 发布于 2017 年 9 月 21 日 .作为 Java8 之后 3 年半才发布的新版本,Java 9 带 来了很多重大的变化其中最重要的改动是 Java 平台模块系统的引入,其他还有诸如 ...

  8. 深入理解JVM内幕:从基本结构到Java 7新特性

    转自:http://www.importnew.com/1486.html 每个Java开发者都知道Java字节码是执行在JRE((Java Runtime Environment Java运行时环境 ...

  9. JVM的运行原理以及JDK 7增加的新特性(二)

    JVM结构 Java编写的代码会按照下图的流程来执行 类装载器装载负责装载编译后的字节码,并加载到运行时数据区(Runtime Data Area),然后执行引擎执行会执行这些字节码. 类加载器(Cl ...

  10. JDK9新特性实战:流关闭新姿势

    做Java开发的都知道,每个资源的打开都需要对应的关闭操作,不然就会使资源一直占用而造成资源浪费,从而降低系统性能. 关于资源的关闭操作,从JDK7-JDK9有了不少的提升及简化. JDK6 在JDK ...

随机推荐

  1. 第128篇:浏览器存储(cookie、webStorage、 IndexedDB)

    好家伙,本篇为<JS高级程序设计>第二五章"浏览器存储"学习笔记 我们先来讲个故事 一个"薅羊毛"的故事 (qq.com) 概括一下,就是 有个人通 ...

  2. 【Azure 事件中心】Azure Event Hub客户端遇见 Expired Heartbeat 错误

    问题描述 Azure Event Hub 在消费数端中,经常性遇见 Expired Heartbeat 错误 (consumer-xxxxxxxxxxxxx-c84873c6c828e8df6c843 ...

  3. 【Azure API 管理】APIM的容量指标(Capacity)数据异常高的情况记录

    问题描述 APIM从标准版降级到基础版,在没有用户使用的情况,Capacity的指标平均显示在80%以上. 这是什么异常情况呢? 问题分析 APIM的容量指标(Capacity)是 API 管理实例中 ...

  4. java 考试易考识记题目(一)

    笔者擅长 C# 语言,4月份要考试,学习 JAVA 是为了考试罢了. 如何在最短时间内学习 JAVA 基础语法和通过考试考核呢~ 学习 JAVA ,要为了应付考试,判断.循环这部分,C.C++.C#. ...

  5. C# 操作国产数据库之【 人大金仓 】之四大模式

    人大金仓优点 人大金仓是国产最主流ORM之一 具有和PgSql一样强悍的性能,同时人大金仓支持了四种数据库模式 : Oracle.PgSql.MySql和SqlServer ,假如你们系统有多种数据库 ...

  6. 为什么Sync.Pool不需要加锁却能保证线程安全

    1. 简介 我们在 Sync.Pool: 提高go语言程序性能的关键一步 一文中,已经了解了使用sync.Pool来实现对象的复用以减少对象的频繁创建和销毁,以及使用sync.Pool的一些常见注意事 ...

  7. Nfs 共享存储搭建

    Nfs 共享存储搭建 为了实现不同操作系统中的数据共享,我们一般会搭建一些用于文件共享的服务器,nfs服务器就是其中一种,它实现的是linux与linux之间的共享.今天我将把如何在linux系统搭建 ...

  8. 使用Zabbix监控openstack的系统资源

    1 概述 OpenStack是一个开源的云计算管理平台项目,是一系列软件开源项目的组合.由NASA(美国国家航空航天局)和Rackspace合作研发并发起,以Apache许可证(Apache软件基金会 ...

  9. 使用fastJson中的JSONObject对象简化POST请求传参-2022新项目

    一.业务场景 Java项目开发中,经常会用到远程调用,不管是POST请求,Feign远程调用,还是使用Resttemplate中的POST方法等等都需要传递参数. 可是如何更好的传递参数呢?之前自己开 ...

  10. 音标 重音 u 用 中文 吁 去记忆 骑马让马停下来的 吁 - 英语

    音标 重音 u 用 中文 吁 去记忆 骑马让马停下来的 吁