当前位置:网站首页>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
边栏推荐
- Classes and objects
- journal
- Nat commun | current progress and open challenges of applied deep learning in Bioscience
- positioner
- Nanotechnology + AI enabled proteomics | Luomi life technology completed nearly ten million US dollars of financing
- Implementation of object detection case based on SSD
- NVIDIA Jetson: GStreamer and openmax (GST OMX) plug-ins
- Pyppeter crawler
- 解决允许在postman中写入注释请求接口方法
- Implement a simple function to calculate the sum of all integers between M ~ n (m < n)
猜你喜欢

positioner

Robocode Tutorial 4 - robocode's game physics

【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))

Solving the problem of displaying too many unique values in ArcGIS partition statistics failed

.105Location

QTableWidget使用讲解

MATLAB小技巧(6)七种滤波方法比较

Nodejs installation

Implementation of image recognition code based on VGg convolutional neural network

【ACM】376. 摆动序列
随机推荐
Robocode tutorial 3 - Robo machine analysis
Permission management with binary
C# 的数据流加密与解密
Flash operates on multiple databases
Nodejs installation
Rust: how to implement a thread pool?
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (1)
journal
[UDS unified diagnostic service] IV. typical diagnostic service (4) - online programming function unit (0x34-0x38)
Realization of consumer gray scale
JD-FreeFuck 京东薅羊毛控制面板 后台命令执行漏洞
Visualization of residential house prices
Install pyshp Library
QT reading and writing XML files (including source code + comments)
纳米技术+AI赋能蛋白质组学|珞米生命科技完成近千万美元融资
Differences between SSD hard disk SATA interface and m.2 interface (detailed summary)
.104History
.105Location
Crawler for querying nicknames and avatars based on qqwebapi
【ACM】509. 斐波那契数(dp五部曲)