当前位置:网站首页>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 !!
正如您所看到的,前三个日志语句中添加了上下文信息 - 而其他两个语句没有此类信息。
参考
边栏推荐
- Test essential tool - postman practical tutorial
- Handsontable數據網格組件
- TypeError: Argument ‘angle‘ can not be treated as a double
- Microbial safety and health, what is biological treatment?
- PHP数组拼接MySQL的in语句
- The argument type 'function' can't be assigned to the parameter type 'void function()‘
- Creating ASCII art with C #
- TypeError: Argument ‘angle‘ can not be treated as a double
- visual studio 2019 快捷键备忘
- Why build a personal blog
猜你喜欢

Qt5 mvc: revealing the secrets of data visualization

使用 C# 创造 ASCII 艺术

6月第4周榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!

Typora的使用

未来的 Web3会带来什么?
![[Qt5 tab] tab label and content hierarchical analysis](/img/cc/c8c2e79877a958f742a8e9e60ceb43.png)
[Qt5 tab] tab label and content hierarchical analysis

Call the classic architecture and build the model based on the classic

物业怎么发短信通知给业主?

农产品换房?“变相”购房补贴!

Connectivity basis of Graphs
随机推荐
TypeError: Argument ‘angle‘ can not be treated as a double
远程办公如何保持高效协同,实现项目稳定增长 |社区征文
TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to
Working for eight years as a programmer, but with a salary of three years after graduation, it's too late to be enlightened again
视频教程 | 长安链推出系列视频教程合集(入门)
Microbial safety and health, what is biological treatment?
Institute of Microbiology, commonly used biochemical reactions in microbiological testing
mysql插入\更新前+判断条件
visual studio 2019 下载
关于白盒测试,这些技巧你得游刃有余~
[queue] 933 Number of Recent Calls
Visual studio 2019 shortcut notes
Some essential differences
gin_gorm
孙宇晨接受瑞士媒体Bilan采访:熊市不会持续太久
qt5-MVC:数据可视化的层次揭秘
一站式洞察行业热点,飞瓜数据B站新功能「流量大盘」上线!
With regard to the white box test, you have to master these skills~
One of the basics - overview of sta Basics
Sun Yuchen told Swiss media Bilan that the bear market will not last long