当前位置:网站首页>Log4j2 threadcontext log link tracking
Log4j2 threadcontext log link tracking
2022-07-01 01:39:00 【Melting pole】
summary
Log4j2 ThreadContext Allows you to mark log statements with multiple unique tags , In order to analyze logs when diagnosing problems at run time - Mainly in multithreaded applications , The application generates a large number of log records in a short time . for example , You may want to scan all logs for a specific user transaction or full session . This process is also known as fish marking ( That is, add some additional context information in each log statement ). This fish marker can help you use Splunk And so on .
Let's see how we can log4j2 Use in ThreadContextclass Fish marking .
stay ThreadContext Add context information to
To uniquely mark each request , please ThreadContext Provide put(String key, String value) The method of accepting the key and its value . You can add as many tags as you need to capture the entire context . Please note that ,ThreadContext All methods of this class are static .
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 !!");
}
}
Or you can put ThreadContext Of Stack Realization and ThreadContext.push(String value) Use it together , As shown below :
//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 !!");
After the transaction ends or context information is no longer required , You can use ThreadContext.clearMap() Method clear information .
ThreadContext Of Stack and Map It is managed by thread ThreadLocal. By way of system Property settings isThreadContextMapInheritable by true, Pass the content of the context mapping to the child thread .
modify log4j2.xml Conversion mode in
<?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>
- Use alone %X contain ThreadContext Everything added .
- Use %X{key} Include the specified key .
- Use %x Include the entire contents of the stack .
Log print results
Now when you run the above code - You will get the following output :
[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 !!
As you can see , Context information has been added to the first three log statements - The other two statements have no such information .
Reference resources
边栏推荐
- php将二维数组元素转为键值对
- PHP converts two-dimensional array elements into key value pairs
- PHP crawls data through third-party plug-ins
- 软件测试的可持续发展,必须要学会敲代码?
- Strictmode analysis registration strictmode principle (4)
- QT web 开发 - video -- 笔记
- 远程办公如何保持高效协同,实现项目稳定增长 |社区征文
- Log logrus third party library usage
- 图的连通性基础
- visual studio 2019 下载
猜你喜欢
随机推荐
编译安装oh-my-zsh
微生物安全與健康,什麼是生物處理?
如何选择券商?另外,手机开户安全么?
C # customize and dynamically switch cursor
Draw some interesting figures with flutter's canvas
流批一体在京东的探索与实践
【模拟】922. Sort Array By Parity II
Strictmode analysis activity leakage -strictmode principle (3)
Connectivity basis of Graphs
小程序中实现excel数据的批量导入
Matlab farthest point sampling (FPS improved version)
数字IC设计流程总结
sort自定义函数
亲测有效,快速创建JMeter桌面快捷方式
"Open math input panel" in MathType editing in win11 is gray and cannot be edited
TypeError: Argument ‘angle‘ can not be treated as a double
laravel+redis 生成订单号-当天从1开始自增
测试必备工具—Postman实战教程
QT web 开发 - video -- 笔记
gin_ gorm








