当前位置:网站首页>Log4j2 cross thread print traceid
Log4j2 cross thread print traceid
2022-04-23 18:11:00 【dawnsun001】
Found in old code , Create asynchronous tasks and pass them manually traceID, Code bloated , It's easy to make mistakes . How do you optimize it ?
The idea is like this , Use Alibaba's TransmittableThreadLocal Expand log4j Of ThreadContextMap. Don't talk much , Direct delivery of dry goods !
- The implementation is as follows
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;
}
}
- In the project resources Next , newly build log4j2.component.properties file , The contents are as follows
log4j2.threadContextMap=com.xxx.xxx.xxxTtlThreadContextMap
Thank you for your patience in reading , I hope it will be of some help to you , Praise is a virtue , I wish you every success in your work Promoting to a higher position !
版权声明
本文为[dawnsun001]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230544289049.html
边栏推荐
- Clion installation tutorial
- 纳米技术+AI赋能蛋白质组学|珞米生命科技完成近千万美元融资
- Multi thread crawling Marco Polo network supplier data
- Read excel, int digital time to time
- Tensorflow tensor introduction
- Flash operates on multiple databases
- ArcGIS license error -15 solution
- PowerDesigner various font settings; Preview font setting; SQL font settings
- 【ACM】509. 斐波那契数(dp五部曲)
- proxy server
猜你喜欢
JD-FreeFuck 京東薅羊毛控制面板 後臺命令執行漏洞
An example of linear regression based on tensorflow
[UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)
Gobang game based on pyGame Library
Data stream encryption and decryption of C
The vivado project corresponding to the board is generated by TCL script
QTableWidget使用讲解
PowerDesigner various font settings; Preview font setting; SQL font settings
解决允许在postman中写入注释请求接口方法
Using files to save data (C language)
随机推荐
k8s之实现redis一主多从动态扩缩容
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (1)
Using files to save data (C language)
Auto.js 自定义对话框
ArcGIS table to excel exceeds the upper limit, conversion failed
由tcl脚本生成板子对应的vivado工程
Resolve the error Max virtual memory areas VM max_ map_ count [65530] is too low, increase to at least [262144]
Installation du docker redis
Rust: the output information of println is displayed during the unit test
What are the relationships and differences between threads and processes
线上怎么确定期货账户安全的?
mysql自动启动设置用Systemctl start mysqld启动
Identification verification code
【ACM】376. 摆动序列
Crawl the product data of cicada mother data platform
Rust: shared variable in thread pool
I / O multiplexing and its related details
_ FindText error
Correct opening method of option
NVIDIA Jetson: GStreamer and openmax (GST OMX) plug-ins