当前位置:网站首页>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
边栏推荐
- Problems and solutions of database migration
- Substring Inversion (Easy Version)
- Rust 的多线程安全引用 Arc
- Framework analysis 2 Source code - login authentication
- 3. Continuous integer
- Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi
- 線性代數第二章-矩陣及其運算
- [leetcode 228] summary interval
- Event listener
- Miscellaneous 1
猜你喜欢
Kalman filter and inertial integrated navigation
Integration and induction of knowledge points of automatic control principle (Han min version)
Database - sorting data
Explanation of login page
Delete and truncate
Preparedstatement prevents SQL injection
Create binary tree
Addition, deletion, modification and query of MySQL table
Paper on LDCT image reconstruction: edge enhancement based transformer for medical image denoising
Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
随机推荐
10.Advance Next Round
IO multiplexing of 09 redis
Explanation of login page
[leetcode 290] word rules
Collection and map thread safety problem solving
Preparedstatement prevents SQL injection
DBCP usage
12. Monkeys climb mountains
Database - sorting data
渔网道路密度计算
Advanced operation of idea debug
Explain of MySQL optimization
Integration and induction of knowledge points of automatic control principle (Han min version)
Framework analysis 1 Introduction to system architecture
Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising
自動控制(韓敏版)
基于pygame库编写的五子棋游戏
Import of data
Delete and truncate
Illustrate the significance of hashcode