<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          如何快速過濾出一次請求的所有日志?

          共 5529字,需瀏覽 12分鐘

           ·

          2021-11-26 21:23

          點(diǎn)擊關(guān)注公眾號(hào),Java干貨及時(shí)送達(dá)

          來源:wudashan.com/2018/02/15/Log-Request-In-MutiThread

          01、前言

          在現(xiàn)網(wǎng)出現(xiàn)故障時(shí),我們經(jīng)常需要獲取一次請求流程里的所有日志進(jìn)行定位。如果請求只在一個(gè)線程里處理,則我們可以通過線程ID來過濾日志,但如果請求包含異步線程的處理,那么光靠線程ID就顯得捉襟見肘了。

          華為IoT平臺(tái),提供了接收設(shè)備上報(bào)數(shù)據(jù)的能力, 當(dāng)數(shù)據(jù)到達(dá)平臺(tái)后,平臺(tái)會(huì)進(jìn)行一些復(fù)雜的業(yè)務(wù)邏輯處理,如數(shù)據(jù)存儲(chǔ),規(guī)則引擎,數(shù)據(jù)推送,命令下發(fā)等等。由于這個(gè)邏輯之間沒有強(qiáng)耦合的關(guān)系,所以通常是異步處理。如何將一次數(shù)據(jù)上報(bào)請求中包含的所有業(yè)務(wù)日志快速過濾出來,就是本文要介紹的。

          02、正文

          SLF4J日志框架提供了一個(gè)MDC(Mapped Diagnostic Contexts)工具類,谷歌翻譯為映射的診斷上下文 ,從字面上很難理解,我們可以先實(shí)戰(zhàn)一把。

          public?class?Main?{

          ????private?static?final?String?KEY?=?"requestId";
          ????private?static?final?Logger?logger?=?LoggerFactory.getLogger(Main.class);
          ????
          ????public?static?void?main(String[]?args)?{

          ????????//?入口傳入請求ID
          ????????MDC.put(KEY,?UUID.randomUUID().toString());
          ????????
          ????????//?打印日志
          ????????logger.debug("log?in?main?thread?1");
          ????????logger.debug("log?in?main?thread?2");
          ????????logger.debug("log?in?main?thread?3");

          ????????//?出口移除請求ID
          ????????MDC.remove(KEY);

          ????}

          }

          我們在main函數(shù)的入口調(diào)用MDC.put()方法傳入請求ID,在出口調(diào)用MDC.remove()方法移除請求ID。配置好log4j2.xml 文件后,運(yùn)行main函數(shù),可以在控制臺(tái)看到以下日志輸出:

          2018-02-17?13:19:52.606?{requestId=f97ea0fb-2a43-40f4-a3e8-711f776857d0}?[main]?DEBUG?cn.wudashan.Main?-?log?in?main?thread?1
          2018-02-17?13:19:52.609?{requestId=f97ea0fb-2a43-40f4-a3e8-711f776857d0}?[main]?DEBUG?cn.wudashan.Main?-?log?in?main?thread?2
          2018-02-17?13:19:52.609?{requestId=f97ea0fb-2a43-40f4-a3e8-711f776857d0}?[main]?DEBUG?cn.wudashan.Main?-?log?in?main?thread?3

          從日志中可以明顯地看到花括號(hào)中包含了 (映射的)請求ID(requestId),這其實(shí)就是我們定位(診斷)問題的關(guān)鍵字(上下文) 。最新面試題整理好了,點(diǎn)擊Java面試庫小程序在線刷題。

          有了MDC工具,只要在接口或切面植入put()remove()代碼,在現(xiàn)網(wǎng)定位問題時(shí),我們就可以通過grep requestId=xxx *.log快速的過濾出某次請求的所有日志。

          03、進(jìn)階

          日志????????logger.debug("log?in?main?thread");????????//?異步線程打印日志????????new?Thread(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread");????????????}????????}).start();????????//?出口移除請求ID????????MDC.remove(KEY);????}}代碼里我們新起了一個(gè)異步線程,并在匿名對象Runnable的run()方法打印日志。運(yùn)行main函數(shù),可以在控制臺(tái)看到以下日志輸出:2018-02-17?14:05:43.487?{requestId=e6099c85-72be-4986-8a28-de6bb2e52b01}?[main]?DEBUG?cn.wudashan.Main?-?log?in?main?thread2018-02-17?14:05:43.490?{}?[Thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread不幸的是,請求ID在異步線程里不打印了。這是怎么回事呢?" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">然而,MDC工具真的有我們所想的這么方便嗎?回到我們開頭,一次請求可能涉及多線程異步處理,那么在多線程異步的場景下,它是否還能正常運(yùn)作呢?Talk is cheap, show me the code。

          推薦一個(gè) Spring Boot 基礎(chǔ)教程及實(shí)戰(zhàn)示例:

          https://github.com/javastacks/spring-boot-best-practice

          日志????????logger.debug("log?in?main?thread");????????//?異步線程打印日志????????new?Thread(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread");????????????}????????}).start();????????//?出口移除請求ID????????MDC.remove(KEY);????}}代碼里我們新起了一個(gè)異步線程,并在匿名對象Runnable的run()方法打印日志。運(yùn)行main函數(shù),可以在控制臺(tái)看到以下日志輸出:2018-02-17?14:05:43.487?{requestId=e6099c85-72be-4986-8a28-de6bb2e52b01}?[main]?DEBUG?cn.wudashan.Main?-?log?in?main?thread2018-02-17?14:05:43.490?{}?[Thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread不幸的是,請求ID在異步線程里不打印了。這是怎么回事呢?" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">public?class?Main?{

          ????private?static?final?String?KEY?=?"requestId";
          ????private?static?final?Logger?logger?=?LoggerFactory.getLogger(Main.class);

          ????public?static?void?main(String[]?args)?{

          ????????//?入口傳入請求ID
          ????????MDC.put(KEY,?UUID.randomUUID().toString());

          ????????//?主線程打印"color:?#1e6bb8;word-wrap:?break-word;font-weight:?bold;border-bottom:?1px?solid">日志
          ????????logger.debug("log?in?main?thread");

          ????????//?異步線程打印"color:?#1e6bb8;word-wrap:?break-word;font-weight:?bold;border-bottom:?1px?solid">日志
          ????????new?Thread(new?Runnable()?{
          ????????????@Override
          ????????????public?void?run()?{
          ????????????????logger.debug("log?in?other?thread");
          ????????????}
          ????????}).start();

          ????????//?出口移除請求ID
          ????????MDC.remove(KEY);

          ????}

          }

          代碼里我們新起了一個(gè)異步線程,并在匿名對象Runnable的run()方法打印日志。?運(yùn)行main函數(shù),可以在控制臺(tái)看到以下日志輸出:

          日志????????logger.debug("log?in?main?thread");????????//?異步線程打印日志????????new?Thread(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread");????????????}????????}).start();????????//?出口移除請求ID????????MDC.remove(KEY);????}}代碼里我們新起了一個(gè)異步線程,并在匿名對象Runnable的run()方法打印日志。運(yùn)行main函數(shù),可以在控制臺(tái)看到以下日志輸出:2018-02-17?14:05:43.487?{requestId=e6099c85-72be-4986-8a28-de6bb2e52b01}?[main]?DEBUG?cn.wudashan.Main?-?log?in?main?thread2018-02-17?14:05:43.490?{}?[Thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread不幸的是,請求ID在異步線程里不打印了。這是怎么回事呢?" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">2018-02-17?14:05:43.487?{requestId=e6099c85-72be-4986-8a28-de6bb2e52b01}?[main]?DEBUG?cn.wudashan.Main?-?log?in?main?thread
          2018-02-17?14:05:43.490?{}?[Thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread

          日志????????logger.debug("log?in?main?thread");????????//?異步線程打印日志????????new?Thread(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread");????????????}????????}).start();????????//?出口移除請求ID????????MDC.remove(KEY);????}}代碼里我們新起了一個(gè)異步線程,并在匿名對象Runnable的run()方法打印日志。運(yùn)行main函數(shù),可以在控制臺(tái)看到以下日志輸出:2018-02-17?14:05:43.487?{requestId=e6099c85-72be-4986-8a28-de6bb2e52b01}?[main]?DEBUG?cn.wudashan.Main?-?log?in?main?thread2018-02-17?14:05:43.490?{}?[Thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread不幸的是,請求ID在異步線程里不打印了。這是怎么回事呢?" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" style="color: rgb(58, 58, 58);" data-linktype="2">不幸的是,請求ID在異步線程里不打印了。這是怎么回事呢?要解決這個(gè)問題,我們就得知道MDC的實(shí)現(xiàn)原理。最新面試題整理好了,點(diǎn)擊Java面試庫小程序在線刷題。

          由于篇幅有限,這里就暫不詳細(xì)介紹,MDC之所以在異步線程中不生效是因?yàn)榈讓硬捎?strong style="line-height: 1.75em;">ThreadLocal 作為數(shù)據(jù)結(jié)構(gòu),我們調(diào)用MDC.put()方法傳入的請求ID只在當(dāng)前線程有效。感興趣的小伙伴可以自己深入一下代碼細(xì)節(jié)。

          知道了原理那么解決這個(gè)問題就輕而易舉了,我們可以使用裝飾器模式 ,新寫一個(gè)MDCRunnable類Runnable接口進(jìn)行一層裝飾。在創(chuàng)建MDCRunnable類時(shí)保存當(dāng)前線程的MDC值,在執(zhí)行run()方法時(shí)再將保存的MDC值拷貝到異步線程中去。

          點(diǎn)擊關(guān)注公眾號(hào),Java干貨及時(shí)送達(dá)

          日志????????logger.debug("log?in?main?thread");????????//?異步線程打印日志,用MDCRunnable裝飾Runnable????????new?Thread(new?MDCRunnable(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread");????????????}????????})).start();????????//?異步線程池打印日志,用MDCRunnable裝飾Runnable????????EXECUTOR.execute(new?MDCRunnable(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread?pool");????????????}????????}));????????EXECUTOR.shutdown();????????//?出口移除請求ID????????MDC.remove(KEY);????}}執(zhí)行main函數(shù),將會(huì)輸出以下日志:2018-03-04?23:44:05.343?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[main]?DEBUG?cn.wudashan.Main?-?log?in?main?thread2018-03-04?23:44:05.346?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[Thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread2018-03-04?23:44:05.347?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[pool-2-thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread?poolCongratulations! 經(jīng)過我們的努力,最終在異步線程和線程池中都有requestId打印了!" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">代碼實(shí)現(xiàn)如下:

          日志????????logger.debug("log?in?main?thread");????????//?異步線程打印日志,用MDCRunnable裝飾Runnable????????new?Thread(new?MDCRunnable(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread");????????????}????????})).start();????????//?異步線程池打印日志,用MDCRunnable裝飾Runnable????????EXECUTOR.execute(new?MDCRunnable(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread?pool");????????????}????????}));????????EXECUTOR.shutdown();????????//?出口移除請求ID????????MDC.remove(KEY);????}}執(zhí)行main函數(shù),將會(huì)輸出以下日志:2018-03-04?23:44:05.343?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[main]?DEBUG?cn.wudashan.Main?-?log?in?main?thread2018-03-04?23:44:05.346?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[Thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread2018-03-04?23:44:05.347?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[pool-2-thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread?poolCongratulations! 經(jīng)過我們的努力,最終在異步線程和線程池中都有requestId打印了!" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">public?class?MDCRunnable?implements?Runnable?{

          ????private?final?Runnable?runnable;

          ????private?final?Map?map;

          ????public?MDCRunnable(Runnable?runnable)?{
          ????????this.runnable?=?runnable;
          ????????//?保存當(dāng)前線程的MDC值
          ????????this.map?=?MDC.getCopyOfContextMap();
          ????}

          ????@Override
          ????public?void?run()?{
          ????????//?傳入已保存的MDC值
          ????????for?(Map.Entry?entry?:?map.entrySet())?{
          ????????????MDC.put(entry.getKey(),?entry.getValue());
          ????????}
          ????????//?裝飾器模式,執(zhí)行run方法
          ????????runnable.run();
          ????????//?移除已保存的MDC值
          ????????for?(Map.Entry?entry?:?map.entrySet())?{
          ????????????MDC.remove(entry.getKey());
          ????????}
          ????}
          ????
          }

          日志????????logger.debug("log?in?main?thread");????????//?異步線程打印日志,用MDCRunnable裝飾Runnable????????new?Thread(new?MDCRunnable(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread");????????????}????????})).start();????????//?異步線程池打印日志,用MDCRunnable裝飾Runnable????????EXECUTOR.execute(new?MDCRunnable(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread?pool");????????????}????????}));????????EXECUTOR.shutdown();????????//?出口移除請求ID????????MDC.remove(KEY);????}}執(zhí)行main函數(shù),將會(huì)輸出以下日志:2018-03-04?23:44:05.343?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[main]?DEBUG?cn.wudashan.Main?-?log?in?main?thread2018-03-04?23:44:05.346?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[Thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread2018-03-04?23:44:05.347?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[pool-2-thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread?poolCongratulations! 經(jīng)過我們的努力,最終在異步線程和線程池中都有requestId打印了!" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">接著,我們需要對main函數(shù)里創(chuàng)建的Runnable實(shí)現(xiàn)類進(jìn)行裝飾:

          日志????????logger.debug("log?in?main?thread");????????//?異步線程打印日志,用MDCRunnable裝飾Runnable????????new?Thread(new?MDCRunnable(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread");????????????}????????})).start();????????//?異步線程池打印日志,用MDCRunnable裝飾Runnable????????EXECUTOR.execute(new?MDCRunnable(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread?pool");????????????}????????}));????????EXECUTOR.shutdown();????????//?出口移除請求ID????????MDC.remove(KEY);????}}執(zhí)行main函數(shù),將會(huì)輸出以下日志:2018-03-04?23:44:05.343?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[main]?DEBUG?cn.wudashan.Main?-?log?in?main?thread2018-03-04?23:44:05.346?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[Thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread2018-03-04?23:44:05.347?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[pool-2-thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread?poolCongratulations! 經(jīng)過我們的努力,最終在異步線程和線程池中都有requestId打印了!" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">public?class?Main?{

          ????private?static?final?String?KEY?=?"requestId";
          ????private?static?final?Logger?logger?=?LoggerFactory.getLogger(Main.class);
          ????private?static?final?ExecutorService?EXECUTOR?=?Executors.newSingleThreadExecutor();

          ????public?static?void?main(String[]?args)?{

          ????????//?入口傳入請求ID
          ????????MDC.put(KEY,?UUID.randomUUID().toString());

          ????????//?主線程打印"color:?#1e6bb8;word-wrap:?break-word;font-weight:?bold;border-bottom:?1px?solid">日志
          ????????logger.debug("log?in?main?thread");

          ????????//?異步線程打印"color:?#1e6bb8;word-wrap:?break-word;font-weight:?bold;border-bottom:?1px?solid">日志,用MDCRunnable裝飾Runnable
          ????????new?Thread(new?MDCRunnable(new?Runnable()?{
          ????????????@Override
          ????????????public?void?run()?{
          ????????????????logger.debug("log?in?other?thread");
          ????????????}
          ????????})).start();

          ????????//?異步線程池打印日志,用MDCRunnable裝飾Runnable
          ????????EXECUTOR.execute(new?MDCRunnable(new?Runnable()?{
          ????????????@Override
          ????????????public?void?run()?{
          ????????????????logger.debug("log?in?other?thread?pool");
          ????????????}
          ????????}));
          ????????EXECUTOR.shutdown();

          ????????//?出口移除請求ID
          ????????MDC.remove(KEY);

          ????}

          }

          日志????????logger.debug("log?in?main?thread");????????//?異步線程打印日志,用MDCRunnable裝飾Runnable????????new?Thread(new?MDCRunnable(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread");????????????}????????})).start();????????//?異步線程池打印日志,用MDCRunnable裝飾Runnable????????EXECUTOR.execute(new?MDCRunnable(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread?pool");????????????}????????}));????????EXECUTOR.shutdown();????????//?出口移除請求ID????????MDC.remove(KEY);????}}執(zhí)行main函數(shù),將會(huì)輸出以下日志:2018-03-04?23:44:05.343?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[main]?DEBUG?cn.wudashan.Main?-?log?in?main?thread2018-03-04?23:44:05.346?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[Thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread2018-03-04?23:44:05.347?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[pool-2-thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread?poolCongratulations! 經(jīng)過我們的努力,最終在異步線程和線程池中都有requestId打印了!" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">執(zhí)行main函數(shù),將會(huì)輸出以下日志

          日志????????logger.debug("log?in?main?thread");????????//?異步線程打印日志,用MDCRunnable裝飾Runnable????????new?Thread(new?MDCRunnable(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread");????????????}????????})).start();????????//?異步線程池打印日志,用MDCRunnable裝飾Runnable????????EXECUTOR.execute(new?MDCRunnable(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread?pool");????????????}????????}));????????EXECUTOR.shutdown();????????//?出口移除請求ID????????MDC.remove(KEY);????}}執(zhí)行main函數(shù),將會(huì)輸出以下日志:2018-03-04?23:44:05.343?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[main]?DEBUG?cn.wudashan.Main?-?log?in?main?thread2018-03-04?23:44:05.346?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[Thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread2018-03-04?23:44:05.347?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[pool-2-thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread?poolCongratulations! 經(jīng)過我們的努力,最終在異步線程和線程池中都有requestId打印了!" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">2018-03-04?23:44:05.343?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[main]?DEBUG?cn.wudashan.Main?-?log?in?main?thread
          2018-03-04?23:44:05.346?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[Thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread
          2018-03-04?23:44:05.347?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[pool-2-thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread?pool

          日志????????logger.debug("log?in?main?thread");????????//?異步線程打印日志,用MDCRunnable裝飾Runnable????????new?Thread(new?MDCRunnable(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread");????????????}????????})).start();????????//?異步線程池打印日志,用MDCRunnable裝飾Runnable????????EXECUTOR.execute(new?MDCRunnable(new?Runnable()?{????????????@Override????????????public?void?run()?{????????????????logger.debug("log?in?other?thread?pool");????????????}????????}));????????EXECUTOR.shutdown();????????//?出口移除請求ID????????MDC.remove(KEY);????}}執(zhí)行main函數(shù),將會(huì)輸出以下日志:2018-03-04?23:44:05.343?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[main]?DEBUG?cn.wudashan.Main?-?log?in?main?thread2018-03-04?23:44:05.346?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[Thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread2018-03-04?23:44:05.347?{requestId=5ee2a117-e090-41d8-977b-cef5dea09d34}?[pool-2-thread-1]?DEBUG?cn.wudashan.Main?-?log?in?other?thread?poolCongratulations! 經(jīng)過我們的努力,最終在異步線程和線程池中都有requestId打印了!" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" style="color: rgb(58, 58, 58);" data-linktype="2">Congratulations! 經(jīng)過我們的努力,最終在異步線程和線程池中都有requestId打印了!

          04、總結(jié)

          本文講述了如何使用MDC工具來快速過濾一次請求的所有日志,并通過裝飾器模式使得MDC工具在異步線程里也能生效。有了MDC,再通過AOP技術(shù)對所有的切面植入requestId,就可以將整個(gè)系統(tǒng)的任意流程的日志過濾出來。

          使用MDC工具,在開發(fā)自測階段,可以極大地節(jié)省定位問題的時(shí)間,提升開發(fā)效率;在運(yùn)維維護(hù)階段,可以快速地收集相關(guān)日志信息,加快分析速度。





          重磅!Spring Boot 2.6 正式發(fā)布。。

          面試官:Java 8 map 和 flatMap 的區(qū)別?
          你還在遍歷搜索集合?Java 8 一行代碼搞定!
          去了家新公司,技術(shù)總監(jiān)不讓用 IntelliJ IDEA!
          重磅!JDK 17 發(fā)布,正式免費(fèi)。。
          別再到處 new 對象了,試試 3 大工廠模式!
          別再寫滿屏的 get & set 了,試試 MapStruct!
          推薦一款代碼神器,代碼量至少省一半!

          Dozer 宣布停止維護(hù),不要再用了。。

          螞蟻面試官:Zookeeper 的選舉流程是怎樣的?
          程序員精通各種技術(shù)體系,45歲求職難!


          關(guān)注Java技術(shù)棧看更多干貨



          獲取 Spring Boot 實(shí)戰(zhàn)筆記!
          瀏覽 32
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  久久夜色精品噜噜亚洲AV | 台湾中文字幕娱乐网 | 操屄视频在线播放 | 国产精品宾馆在线精品酒店 | 影音先锋色成人 |