当前位置:网站首页>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
边栏推荐
- 【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))
- String function in MySQL
- Nodejs installation
- NVIDIA Jetson: GStreamer and openmax (GST OMX) plug-ins
- Data stream encryption and decryption of C
- Build openstack platform
- 软件测试总结
- .105Location
- 解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
- Crawling mobile game website game details and comments (MQ + multithreading)
猜你喜欢
Clion installation tutorial
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (1)
k8s之实现redis一主多从动态扩缩容
Robocode tutorial 8 - advanced robot
A few lines of code teach you to crawl lol skin pictures
Auto. JS custom dialog box
Random number generation of C #
Implementation of image recognition code based on VGg convolutional neural network
Data stream encryption and decryption of C
ArcGIS license error -15 solution
随机推荐
Multi thread crawling Marco Polo network supplier data
Nanotechnology + AI enabled proteomics | Luomi life technology completed nearly ten million US dollars of financing
Climbing watermelon video URL
MATLAB从入门到精通(二)
Svn simple operation command
【ACM】509. 斐波那契数(dp五部曲)
Gobang game based on pyGame Library
解决允许在postman中写入注释请求接口方法
Qtablewidget usage explanation
Install the yapiupload plug-in in idea and upload the API interface to the Yapi document
Rust: shared variable in thread pool
Correct opening method of option
Implement a simple function to calculate the sum of all integers between M ~ n (m < n)
Flash operates on multiple databases
Nodejs installation
Nat commun | current progress and open challenges of applied deep learning in Bioscience
[UDS unified diagnostic service] v. diagnostic application example: Flash bootloader
Batch export ArcGIS attribute table
Auto. JS custom dialog box
C#字节数组(byte[])和字符串相互转换