当前位置:网站首页>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
边栏推荐
- H. Are You Safe? Convex hull naked problem
- 电机与拖动(戚金清版)学习整理
- What is the difference between the basic feasible solution and the basic feasible solution in linear programming?
- Busybox initrd and initialization process
- Chapter 4 of line generation - linear correlation of vector systems
- 2. Devops sonar installation
- GNU EFI header file
- 从源代码到可执行文件的过程
- C array
- [leetcode 19] delete the penultimate node of the linked list
猜你喜欢
Illustrate the significance of hashcode
Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()
A general U-shaped transformer for image restoration
Storing inherited knowledge in cloud computing
Linear algebra Chapter 1 - determinant
Algèbre linéaire chapitre 1 - déterminants
lambda expressions
Algèbre linéaire chapitre 2 - matrice et son fonctionnement
Explain of MySQL optimization
[leetcode 67] sum of two binary numbers
随机推荐
渔网道路密度计算
Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising
Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()
Addition, deletion, modification and query of MySQL table
-- SQL query and return limit rows
Create binary tree
Motor and drive (Qi Jinqing Edition)
What is the difference between the basic feasible solution and the basic feasible solution in linear programming?
MySQL basic madness theory
Example of reentrant lock thread waiting to wake up
[leetcode 383] ransom letter
Understanding and installing MySQL
A sharp tool to improve work efficiency
Qthread simple test understanding
Stability building best practices
Type conversion in C #
Common programming records - parser = argparse ArgumentParser()
檢測技術與原理
[leetcode 459] duplicate substring
Export of data