当前位置:网站首页>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
边栏推荐
- Install pyshp Library
- 由tcl脚本生成板子对应的vivado工程
- Solving the problem of displaying too many unique values in ArcGIS partition statistics failed
- An example of linear regression based on tensorflow
- Cells in rust share variable pointers
- Batch export ArcGIS attribute table
- 登录和发布文章功能测试
- Rust: how to implement a thread pool?
- C language array processing batch data
- cv_ Solution of mismatch between bridge and opencv
猜你喜欢
Robocode tutorial 8 - advanced robot
The vivado project corresponding to the board is generated by TCL script
C medium? This form of
QTableWidget使用讲解
Random number generation of C #
positioner
GDAL + ogr learning
【ACM】455. 分发饼干(1. 大饼干优先喂给大胃口;2. 遍历两个数组可以只用一个for循环(用下标索引--来遍历另一个数组))
Solving the problem of displaying too many unique values in ArcGIS partition statistics failed
Deep learning classic network analysis and target detection (I): r-cnn
随机推荐
2022 Jiangxi Photovoltaic Exhibition, China distributed Photovoltaic Exhibition, Nanchang solar energy utilization Exhibition
C# 网络相关操作
Deep learning classic network analysis and target detection (I): r-cnn
.105Location
MySQL 中的字符串函数
I/O多路复用及其相关详解
Implementation of image recognition code based on VGg convolutional neural network
Flash operates on multiple databases
Qt读写XML文件(含源码+注释)
JD-FreeFuck 京東薅羊毛控制面板 後臺命令執行漏洞
【ACM】455. Distribute Biscuits (1. Give priority to big biscuits to big appetite; 2. Traverse two arrays with only one for loop (use subscript index -- to traverse another array))
JD freefuck Jingdong HaoMao control panel background Command Execution Vulnerability
Climbing watermelon video URL
软件测试总结
Selenium + webdriver + chrome realize Baidu to search for pictures
Resolves the interface method that allows annotation requests to be written in postman
QT reading and writing XML files (including source code + comments)
Secure credit
Docker 安裝 Redis
Rust: how to match a string?