当前位置:网站首页>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
边栏推荐
- Arcpy adds fields and loop assignments to vector data
- MySQL 中的字符串函数
- Docker installation MySQL
- Realization of consumer gray scale
- MATLAB小技巧(6)七种滤波方法比较
- Implementation of k8s redis one master multi slave dynamic capacity expansion
- Auto.js 自定义对话框
- Rust: how to implement a thread pool?
- Tensorflow tensor introduction
- Correct opening method of option
猜你喜欢
Gobang game based on pyGame Library
Vulnérabilité d'exécution de la commande de fond du panneau de commande JD - freefuck
解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
Install pyshp Library
GDAL + ogr learning
Transfer learning of five categories of pictures based on VGg
Nodejs installation
Robocode Tutorial 4 - robocode's game physics
Visualization of residential house prices
C#的随机数生成
随机推荐
Nat Commun|在生物科学领域应用深度学习的当前进展和开放挑战
C [file operation] read TXT text by line
xlsxwriter. exceptions. Filecreateerror: [errno 13] permission denied
The difference between deep copy and shallow copy
7-21 wrong questions involve knowledge points.
2022 Jiangxi energy storage technology exhibition, China Battery exhibition, power battery exhibition and fuel cell Exhibition
C language array processing batch data
Classes and objects
【ACM】509. 斐波那契数(dp五部曲)
Excel opens large CSV format data
Svn simple operation command
Resolves the interface method that allows annotation requests to be written in postman
纳米技术+AI赋能蛋白质组学|珞米生命科技完成近千万美元融资
From source code to executable file
Map basemap Library
ROS package NMEA_ navsat_ Driver reads GPS and Beidou Positioning Information Notes
Queue solving Joseph problem
Qt读写XML文件(含源码+注释)
Crawling mobile game website game details and comments (MQ + multithreading)
Rust: how to implement a thread pool?