欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Logback MDCAdapter日志跟蹤及自定義效果源碼解讀

 更新時(shí)間:2023年11月12日 09:02:42   作者:codecraft  
這篇文章主要為大家介紹了Logback MDCAdapter日志跟蹤及自定義效果源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

本文主要研究一下LogbackMDCAdapter

MDCAdapter

org/slf4j/spi/MDCAdapter.java

public interface MDCAdapter {
    /**
     * Put a context value (the <code>val</code> parameter) as identified with
     * the <code>key</code> parameter into the current thread's context map. 
     * The <code>key</code> parameter cannot be null. The <code>val</code> parameter
     * can be null only if the underlying implementation supports it.
     * 
     * <p>If the current thread does not have a context map it is created as a side
     * effect of this call.
     */
    public void put(String key, String val);
    /**
     * Get the context identified by the <code>key</code> parameter.
     * The <code>key</code> parameter cannot be null.
     * 
     * @return the string value identified by the <code>key</code> parameter.
     */
    public String get(String key);
    /**
     * Remove the context identified by the <code>key</code> parameter.
     * The <code>key</code> parameter cannot be null. 
     * 
     * <p>
     * This method does nothing if there is no previous value 
     * associated with <code>key</code>.
     */
    public void remove(String key);
    /**
     * Clear all entries in the MDC.
     */
    public void clear();
    /**
     * Return a copy of the current thread's context map, with keys and 
     * values of type String. Returned value may be null.
     * 
     * @return A copy of the current thread's context map. May be null.
     * @since 1.5.1
     */
    public Map<String, String> getCopyOfContextMap();
    /**
     * Set the current thread's context map by first clearing any existing 
     * map and then copying the map passed as parameter. The context map 
     * parameter must only contain keys and values of type String.
     * 
     * Implementations must support null valued map passed as parameter.
     * 
     * @param contextMap must contain only keys and values of type String
     * 
     * @since 1.5.1
     */
    public void setContextMap(Map<String, String> contextMap);
    /**
     * Push a value into the deque(stack) referenced by 'key'.
     *      
     * @param key identifies the appropriate stack
     * @param value the value to push into the stack
     * @since 2.0.0
     */
    public void pushByKey(String key, String value);
    /**
     * Pop the stack referenced by 'key' and return the value possibly null.
     * 
     * @param key identifies the deque(stack)
     * @return the value just popped. May be null/
     * @since 2.0.0
     */
    public String popByKey(String key);
    /**
     * Returns a copy of the deque(stack) referenced by 'key'. May be null.
     * 
     * @param key identifies the  stack
     * @return copy of stack referenced by 'key'. May be null.
     * 
     * @since 2.0.0
     */
    public Deque<String>  getCopyOfDequeByKey(String key);
    /**
     * Clear the deque(stack) referenced by 'key'. 
     * 
     * @param key identifies the  stack
     * 
     * @since 2.0.0
     */
    public void clearDequeByKey(String key);
}
slf4j定義了MDCAdapter接口,該接口定義了put、get、remove、clear、getCopyOfContextMap、setContextMap、pushByKey、popByKey、getCopyOfDequeByKey、clearDequeByKey方法

LogbackMDCAdapter

ch/qos/logback/classic/util/LogbackMDCAdapter.java

public class LogbackMDCAdapter implements MDCAdapter  {
    // BEWARE: Keys or values placed in a ThreadLocal should not be of a type/class
    // not included in the JDK. See also https://jira.qos.ch/browse/LOGBACK-450
    final ThreadLocal<Map<String, String>> readWriteThreadLocalMap = new ThreadLocal<Map<String, String>>();
    final ThreadLocal<Map<String, String>> readOnlyThreadLocalMap = new ThreadLocal<Map<String, String>>();
    private final ThreadLocalMapOfStacks threadLocalMapOfDeques = new ThreadLocalMapOfStacks();
    //......
}
LogbackMDCAdapter實(shí)現(xiàn)了MDCAdapter接口,它基于readWriteThreadLocalMap、readOnlyThreadLocalMap、threadLocalMapOfDeques來實(shí)現(xiàn)

readWriteThreadLocalMap

public void put(String key, String val) throws IllegalArgumentException {
        if (key == null) {
            throw new IllegalArgumentException("key cannot be null");
        }
        Map<String, String> current = readWriteThreadLocalMap.get();
        if (current == null) {
            current = new HashMap<String, String>();
            readWriteThreadLocalMap.set(current);
        }
        current.put(key, val);
        nullifyReadOnlyThreadLocalMap();
    }
    @Override
    public String get(String key) {
        Map<String, String> hashMap = readWriteThreadLocalMap.get();
        if ((hashMap != null) && (key != null)) {
            return hashMap.get(key);
        } else {
            return null;
        }
    }
    @Override
    public void remove(String key) {
        if (key == null) {
            return;
        }
        Map<String, String> current = readWriteThreadLocalMap.get();
        if (current != null) {
            current.remove(key);
            nullifyReadOnlyThreadLocalMap();
        }
    }
    @Override
    public void clear() {
        readWriteThreadLocalMap.set(null);
        nullifyReadOnlyThreadLocalMap();
    }
    private void nullifyReadOnlyThreadLocalMap() {
        readOnlyThreadLocalMap.set(null);
    }  
    public void setContextMap(Map contextMap) {
        if (contextMap != null) {
            readWriteThreadLocalMap.set(new HashMap<String, String>(contextMap));
        } else {
            readWriteThreadLocalMap.set(null);
        }
        nullifyReadOnlyThreadLocalMap();
    }
put、get、remove、clear、setContextMap都是基于readWriteThreadLocalMap,同時(shí)修改操作會(huì)同時(shí)調(diào)用nullifyReadOnlyThreadLocalMap,將readOnlyThreadLocalMap設(shè)置為null

getCopyOfContextMap

public Map getCopyOfContextMap() {
        Map<String, String> readOnlyMap = getPropertyMap();
        if (readOnlyMap == null) {
            return null;
        } else {
            return new HashMap<String, String>(readOnlyMap);
        }
    }

    public Map<String, String> getPropertyMap() {
        Map<String, String> readOnlyMap = readOnlyThreadLocalMap.get();
        if (readOnlyMap == null) {
            Map<String, String> current = readWriteThreadLocalMap.get();
            if (current != null) {
                final Map<String, String> tempMap = new HashMap<String, String>(current);
                readOnlyMap = Collections.unmodifiableMap(tempMap);
                readOnlyThreadLocalMap.set(readOnlyMap);
            }
        }
        return readOnlyMap;
    }
getCopyOfContextMap方法通過getPropertyMap獲取,如果不為null則新創(chuàng)建HashMap返回;getPropertyMap先從readOnlyThreadLocalMap讀取,如果readOnlyThreadLocalMap為null則從readWriteThreadLocalMap拷貝一份unmodifiableMap,并設(shè)置到readOnlyThreadLocalMap

threadLocalMapOfDeques

@Override
    public void pushByKey(String key, String value) {
        threadLocalMapOfDeques.pushByKey(key, value);
    }
    @Override
    public String popByKey(String key) {
        return threadLocalMapOfDeques.popByKey(key);
    }
    @Override
    public Deque<String> getCopyOfDequeByKey(String key) {
        return threadLocalMapOfDeques.getCopyOfDequeByKey(key);
    }
    @Override
    public void clearDequeByKey(String key) {
        threadLocalMapOfDeques.clearDequeByKey(key);
    }
pushByKey、popByKey、getCopyOfDequeByKey、clearDequeByKey均是基于threadLocalMapOfDeques,它是ThreadLocalMapOfStacks類型

ThreadLocalMapOfStacks

org/slf4j/helpers/ThreadLocalMapOfStacks.java

public class ThreadLocalMapOfStacks {
    // BEWARE: Keys or values placed in a ThreadLocal should not be of a type/class
    // not included in the JDK. See also https://jira.qos.ch/browse/LOGBACK-450
    final ThreadLocal<Map<String, Deque<String>>> tlMapOfStacks = new ThreadLocal<>();
    public void pushByKey(String key, String value) {
        if (key == null)
            return;
        Map<String, Deque<String>> map = tlMapOfStacks.get();
        if (map == null) {
            map = new HashMap<>();
            tlMapOfStacks.set(map);
        }
        Deque<String> deque = map.get(key);
        if (deque == null) {
            deque = new ArrayDeque<>();
        }
        deque.push(value);
        map.put(key, deque);
    }
    public String popByKey(String key) {
        if (key == null)
            return null;
        Map<String, Deque<String>> map = tlMapOfStacks.get();
        if (map == null)
            return null;
        Deque<String> deque = map.get(key);
        if (deque == null)
            return null;
        return deque.pop();
    }
    public Deque<String> getCopyOfDequeByKey(String key) {
        if (key == null)
            return null;
        Map<String, Deque<String>> map = tlMapOfStacks.get();
        if (map == null)
            return null;
        Deque<String> deque = map.get(key);
        if (deque == null)
            return null;
        return new ArrayDeque<String>(deque);
    }
    /**
     * Clear the deque(stack) referenced by 'key'. 
     * 
     * @param key identifies the  stack
     * 
     * @since 2.0.0
     */
    public void clearDequeByKey(String key) {
        if (key == null)
            return;
        Map<String, Deque<String>> map = tlMapOfStacks.get();
        if (map == null)
            return;
        Deque<String> deque = map.get(key);
        if (deque == null)
            return;
        deque.clear();
    }
}
ThreadLocalMapOfStacks是slf4j定義的,基于ThreadLocal<Map<String, Deque<String>>>實(shí)現(xiàn)的

小結(jié)

slf4j定義了MDCAdapter接口,該接口定義了put、get、remove、clear、getCopyOfContextMap、setContextMap、pushByKey、popByKey、getCopyOfDequeByKey、clearDequeByKey方法;LogbackMDCAdapter實(shí)現(xiàn)了MDCAdapter接口,它基于readWriteThreadLocalMap、readOnlyThreadLocalMap、threadLocalMapOfDeques來實(shí)現(xiàn),其中put、get、remove、clear、setContextMap都是基于readWriteThreadLocalMap,pushByKey、popByKey、getCopyOfDequeByKey、clearDequeByKey均是基于threadLocalMapOfDeques。

以上就是Logback MDCAdapter日志跟蹤及自定義效果源碼解讀的詳細(xì)內(nèi)容,更多關(guān)于Logback MDCAdapter日志跟蹤的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java里得到00:00:00格式的時(shí)分秒的Timestamp

    Java里得到00:00:00格式的時(shí)分秒的Timestamp

    Java里如何得到00:00:00格式的時(shí)分秒的Timestamp ,下面是具體的實(shí)現(xiàn)代碼,需要的朋友可以參考下。
    2009-09-09
  • SpringBoot環(huán)境Druid數(shù)據(jù)源使用及特點(diǎn)

    SpringBoot環(huán)境Druid數(shù)據(jù)源使用及特點(diǎn)

    Druid 是目前比較流行的高性能的,分布式列存儲(chǔ)的OLAP框架(具體來說是MOLAP)。本文給大家分享SpringBoot環(huán)境Druid數(shù)據(jù)源使用及特點(diǎn)介紹,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • 詳解Java?缺失的特性擴(kuò)展方法

    詳解Java?缺失的特性擴(kuò)展方法

    這篇文章主要為大家介紹了Java?缺失的特性擴(kuò)展方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • SpringBoot如何讀取配置文件中的數(shù)據(jù)到map和list

    SpringBoot如何讀取配置文件中的數(shù)據(jù)到map和list

    這篇文章主要介紹了SpringBoot如何讀取配置文件中的數(shù)據(jù)到map和list,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • SpringBoot連接MYSQL數(shù)據(jù)庫并使用JPA進(jìn)行操作

    SpringBoot連接MYSQL數(shù)據(jù)庫并使用JPA進(jìn)行操作

    今天給大家介紹一下如何SpringBoot中連接Mysql數(shù)據(jù)庫,并使用JPA進(jìn)行數(shù)據(jù)庫的相關(guān)操作。
    2017-04-04
  • MyBatis入門學(xué)習(xí)教程(一)-MyBatis快速入門

    MyBatis入門學(xué)習(xí)教程(一)-MyBatis快速入門

    MyBatis是一個(gè)支持普通SQL查詢,存儲(chǔ)過程和高級映射的優(yōu)秀持久層框架,這篇文章主要給大家分享MyBatis入門學(xué)習(xí)教程(一)-MyBatis快速入門,需要的朋友可以參考下
    2015-08-08
  • 深入理解Spring中的循環(huán)依賴

    深入理解Spring中的循環(huán)依賴

    Spring在注入bean的時(shí)候會(huì)做循環(huán)依賴檢查,下面這篇文章主要給大家介紹了關(guān)于Spring中循環(huán)依賴的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧
    2018-05-05
  • Java常見面試題之多線程和高并發(fā)詳解

    Java常見面試題之多線程和高并發(fā)詳解

    這篇文章主要給大家介紹了關(guān)于Java面試題之多線程和高并發(fā)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 深入探究Spring底層核心原理

    深入探究Spring底層核心原理

    理解IOC與AOP的實(shí)現(xiàn)機(jī)制,優(yōu)化應(yīng)用性能與可維護(hù)性。Spring通過IOC容器管理Bean,AOP實(shí)現(xiàn)切面編程,支持事務(wù)管理、ORM框架等。深入理解Spring原理,可以幫助我們更好地使用Spring框架,提高開發(fā)效率與質(zhì)量
    2023-04-04
  • Java中的@PostConstruct注解用法詳解

    Java中的@PostConstruct注解用法詳解

    @PostConstruct注解是Java中一個(gè)強(qiáng)大的特性,它允許開發(fā)人員在Bean被構(gòu)造并且依賴被注入后執(zhí)行初始化邏輯,本文將從源碼和用法的角度深入解析@PostConstruct注解,探討其實(shí)現(xiàn)細(xì)節(jié)和實(shí)際應(yīng)用
    2023-07-07

最新評論