当前位置:网站首页>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 !

  1. 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;
    }
}
  1. 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