当前位置:网站首页>Log4j2跨线程打印traceId
Log4j2跨线程打印traceId
2022-04-23 05:45:00 【dawnsun001】
发现旧代码中,创建异步任务手动传递traceID,代码臃肿,容易出错。如何优化呢?
大概思路是这样的 ,使用阿里的TransmittableThreadLocal 扩展log4j的ThreadContextMap。话不多说,直接上干货!
- 实现如下
public class TtlThreadContextMap implements ThreadContextMap {
private final ThreadLocal<Map<String, String>> localMap;
public TtlThreadContextMap() {
this.localMap = new TransmittableThreadLocal<Map<String, String>>();
}
@Override
public void put(final String key, final String value) {
Map<String, String> map = localMap.get();
map = map == null ? new HashMap<String, String>() : new HashMap<String, String>(map);
map.put(key, value);
localMap.set(Collections.unmodifiableMap(map));
}
@Override
public String get(final String key) {
final Map<String, String> map = localMap.get();
return map == null ? null : map.get(key);
}
@Override
public void remove(final String key) {
final Map<String, String> map = localMap.get();
if (map != null) {
final Map<String, String> copy = new HashMap<String, String>(map);
copy.remove(key);
localMap.set(Collections.unmodifiableMap(copy));
}
}
@Override
public void clear() {
localMap.remove();
}
@Override
public boolean containsKey(final String key) {
final Map<String, String> map = localMap.get();
return map != null && map.containsKey(key);
}
@Override
public Map<String, String> getCopy() {
final Map<String, String> map = localMap.get();
return map == null ? new HashMap<String, String>() : new HashMap<String, String>(map);
}
@Override
public Map<String, String> getImmutableMapOrNull() {
return localMap.get();
}
@Override
public boolean isEmpty() {
final Map<String, String> map = localMap.get();
return map == null || map.size() == 0;
}
@Override
public String toString() {
final Map<String, String> map = localMap.get();
return map == null ? "{}" : map.toString();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
final Map<String, String> map = this.localMap.get();
result = prime * result + ((map == null) ? 0 : map.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof TtlThreadContextMap)) {
return false;
}
final TtlThreadContextMap other = (TtlThreadContextMap) obj;
final Map<String, String> map = this.localMap.get();
final Map<String, String> otherMap = other.getImmutableMapOrNull();
if (map == null) {
if (otherMap != null) {
return false;
}
} else if (!map.equals(otherMap)) {
return false;
}
return true;
}
}
- 在项目resources下,新建log4j2.component.properties文件,内容如下
log4j2.threadContextMap=com.xxx.xxx.xxxTtlThreadContextMap
感谢您的耐心阅读,希望对您有所帮助,点赞是一种美德,祝您工作顺利 步步高升!
版权声明
本文为[dawnsun001]所创,转载请带上原文链接,感谢
https://blog.csdn.net/dawnsun2013/article/details/124329701
边栏推荐
- Basic knowledge of network in cloud computing
- 5.The Simple Problem
- Why does the subscript of the array start from 0 instead of 1?
- Kalman filter and inertial integrated navigation
- Usage scenario of copyonwritearraylist
- 程序设计训练
- Common sense of thread pool
- A general U-shaped transformer for image restoration
- 4. Print form
- Comparative study paper - [Moco, cvpr2020] momentum contract for unsupervised visual representation learning
猜你喜欢
![Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images](/img/9d/487c77b5d25d3e37fb629164c804e2.png)
Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images

Import of data

Delete and truncate

Motor and drive (Qi Jinqing Edition)

检测技术与原理

Chapter 4 of line generation - linear correlation of vector systems

Why does the subscript of the array start from 0 instead of 1?

-- SQL query and return limit rows

Practical operation - Nacos installation and configuration

Algèbre linéaire chapitre 1 - déterminants
随机推荐
LockSupport. Park and unpark, wait and notify
Basic knowledge of network in cloud computing
Addition, deletion, query and modification of data
SQL optimization best practices
MySQL basic madness theory
Stability building best practices
[leetcode 401] binary Watch
Installation and usage skills of idea
Troubleshooting of data deleted and reappeared problems
Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code
SQL injection
自动控制原理知识点整合归纳(韩敏版)
[leetcode 67] sum of two binary numbers
Rust 的多线程安全引用 Arc
St table template
GNU EFI header file
@Problems caused by internal dead loop of postconstruct method
Calculation (enter the calculation formula to get the result)
Introduction to virtualization features
MySQL table constraints and table design