当前位置:网站首页>Log4j2 ThreadContext日志链路追踪
Log4j2 ThreadContext日志链路追踪
2022-07-01 01:01:00 【融极】
概述
Log4j2 ThreadContext允许您使用多个唯一标记标记日志语句,以便在运行时诊断问题时分析日志 - 主要是在多线程应用程序中,应用程序在短时间内生成大量日志记录。例如,您可能希望扫描特定用户事务或完整会话的所有日志。此过程也称为鱼标记(即在每个日志语句中添加一些额外的上下文信息)。这种鱼标记可以帮助使用Splunk等日志的自动化工具。
让我们看看我们如何在log4j2中使用ThreadContextclass进行鱼类标记。
在ThreadContext中添加上下文信息
要唯一标记每个请求,请ThreadContext提供put(String key, String value)接受密钥及其值的方法。您可以根据需要添加任意数量的标记来捕获整个上下文信息。请注意,ThreadContext该类的所有方法都是静态的。
package com.howtodoinjava.log4j2.examples;
import java.util.UUID;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
public class Log4j2HelloWorldExample
{
private static final Logger LOGGER = LogManager.getLogger(Log4j2HelloWorldExample.class.getName());
public static void main(String[] args)
{
//Add context information
ThreadContext.put("id", UUID.randomUUID().toString());
ThreadContext.put("ipAddress", "192.168.21.9");
LOGGER.debug("Debug Message Logged !!");
LOGGER.info("Info Message Logged !!");
LOGGER.debug("Another Debug Message !!");
//Clear the map
ThreadContext.clearMap();
LOGGER.debug("Thread Context Cleaned up !!");
LOGGER.debug("Log message with no context information !!");
}
}
或者您可以将ThreadContext的Stack实现与ThreadContext.push(String value)一起使用,如下所示:
//Add context information
ThreadContext.push(UUID.randomUUID().toString());
ThreadContext.push("192.168.21.9");
LOGGER.debug("Debug Message Logged !!");
LOGGER.info("Info Message Logged !!");
LOGGER.debug("Another Debug Message !!");
//Clear the map
ThreadContext.clearStack();
LOGGER.debug("Thread Context Cleaned up !!");
LOGGER.debug("Log message with no context information !!");
事务结束或不再需要上下文信息后,您可以使用ThreadContext.clearMap()方法清空信息。
ThreadContext的Stack和Map是按线程管理的ThreadLocal。通过将system属性设置isThreadContextMapInheritable为true,将将上下文映射的内容传递给子线程。
修改log4j2.xml中的转换模式
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%X{id}] [%X{ipAddress}] %c{1} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="debug" additivity="false">
<AppenderRef ref="console" />
</Root>
</Loggers>
</Configuration>
- 单独使用%X包含ThreadContext添加的全部内容。
- 使用%X{key}包括指定的键。
- 使用%x包括堆栈的全部内容。
日志打印结果
现在当你运行上面的代码时 - 你会得到以下输出:
[DEBUG] 2016-06-21 13:09:56.485 [main] [7cdd4cf0-2c26-4b81-b374-1adce3781499] [192.168.21.9] Log4j2HelloWorldExample - Debug Message Logged !!
[INFO ] 2016-06-21 13:09:56.487 [main] [7cdd4cf0-2c26-4b81-b374-1adce3781499] [192.168.21.9] Log4j2HelloWorldExample - Info Message Logged !!
[DEBUG] 2016-06-21 13:09:56.487 [main] [7cdd4cf0-2c26-4b81-b374-1adce3781499] [192.168.21.9] Log4j2HelloWorldExample - Another Debug Message !!
[DEBUG] 2016-06-21 13:09:56.487 [main] [] [] Log4j2HelloWorldExample - Thread Context Cleaned up !!
[DEBUG] 2016-06-21 13:09:56.487 [main] [] [] Log4j2HelloWorldExample - Log message with no context information !!
正如您所看到的,前三个日志语句中添加了上下文信息 - 而其他两个语句没有此类信息。
参考
边栏推荐
- Service grid ASM year end summary: how do end users use the service grid?
- One of the basics - overview of sta Basics
- [simulation] 922 Sort Array By Parity II
- System settings large page
- laravel 事件 & 订阅
- 工作6年,来盘点一下职场人混迹职场的黄金法则
- 做生意更加务实
- 3dsmax plug-in development traversal node object and object acquisition and inode transformation matrix description
- 元宇宙为 VR/AR 带来的新机会
- 【多源bfs】934. Shortest Bridge
猜你喜欢
[Qt5 tab] tab label and content hierarchical analysis
【Qt5-基础篇】随机数显示屏展示
农产品换房?“变相”购房补贴!
45 year old programmer tells you: why do programmers want to change jobs? It's too true
数据探索电商平台用户行为流失分析
视频教程 | 长安链推出系列视频教程合集(入门)
Using recyclerreview to show banner is very simple
Use of typora
Compile and install oh my Zsh
PHP通过第三方插件爬取数据
随机推荐
【多源bfs】934. Shortest Bridge
TypeError: Argument ‘angle‘ can not be treated as a double
医疗HIS行业短信发送解决方案
直播商城源码,实现左右联动商品分类页面
zabbix如何配置告警短信?(预警短信通知设置流程)
sort自定义函数
未来的 Web3会带来什么?
Construction and beautification of personal blog
Microbial safety and health, what is biological treatment?
流批一体在京东的探索与实践
gin_ gorm
Digital IC design process summary
Working for eight years as a programmer, but with a salary of three years after graduation, it's too late to be enlightened again
45岁程序员告诉你:程序员为什么要跳槽,太真实...
The personal test is effective, and the JMeter desktop shortcut is quickly created
【队列】933. Number of Recent Calls
MFC TCP communication server client demo notes vs2019
3dsmax插件开发遍历节点对象和Object获取及INode变换矩阵说明
Zero of DC learning notes -- overview and basic process introduction
【栈】921. Minimum Add to Make Parentheses Valid