什么是MDC机制

MDC(Mapped Diagnostic Contexts)映射诊断上下文,主要用在做日志链路跟踪时,动态配置用户自定义的一些信息,比如requestId、sessionId等等。MDC使用的容器支持多线程操作,满足线程安全。

MDC的使用

  • pom.xml依赖
<!-- 日志log4j2 -->
<dependency><groupId>com.lmax</groupId><artifactId>disruptor</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

此处日志具体实现采用的是log4j2,依赖slf4j-api 版本1.7.25

  • log4j2配置日志输出
<!-- 控制台 -->
<Console name="Console" target="SYSTEM_OUT"><PatternLayoutpattern="%d{yyyy-MM-dd HH:mm:ss.SSS} - %X{requestId} %-5p %t [%c:%L]-%m%n"/>
</Console>

在patternLayout中 %X{requestId} 显示我们定义的变量
日志配置详解可以戳这里:【系统日志笔记一】——丰富Log4j2配置

  • MainLogAspect.java
package com.lucas.device.aop;import javax.servlet.http.HttpServletRequest;import com.phlico.common.framework.tool.unique.IdWorkerUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import lombok.extern.slf4j.Slf4j;/*** <Description> 系统日志切面<br>** @author xubin<br>* @version 1.0<br>* @taskId <br>* @CreateDate 2019/4/12 <br>*/@Aspect
@Slf4j
@Component
public class MainLogAspect {/*** Description: 自定义切点<br>* * @author xubin <br>* @taskId <br>*/@Pointcut("@annotation(com.lucas.device.aop.Logc)")public void pointCut() {}/*** Description: 前置通知-记录请求信息<br>* * @author xubin <br>* @taskId <br>* @param joinPoint <br>*/@Before("pointCut()")public void doBeforeAdvice(JoinPoint joinPoint) {Signature signature = joinPoint.getSignature();ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = sra.getRequest();// 获取目标方法的参数信息Object[] obj = joinPoint.getArgs();// MDC容器增加requestIdMDC.put("requestId", IdWorkerUtil.getFlowIdWorkerInstance().nextId());log.info("[MainLogAspect]-request url:{}, class: {}, method: {}, param: {}", request.getRequestURI(), signature.getDeclaringTypeName(), signature.getName(), obj[0].toString());}/*** Description: 后置通知-记录返回信息<br>* * @author xubin <br>* @taskId <br>* @param joinPoint <br>* @param result <br>*/@AfterReturning(returning = "result", pointcut = "pointCut()")public void doAfterReturningAdvice(JoinPoint joinPoint, Object result) {Signature signature = joinPoint.getSignature();ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = sra.getRequest();log.info("[MainLogAspect]-response url:{}, class: {}, method: {}, param: {}", request.getRequestURI(), signature.getDeclaringTypeName(), signature.getName(), result.toString());// MDC容器移除requestIdMDC.remove("requestId");}/*** Description: 后置异常通知-记录返回出现异常<br>* * @author xubin <br>* @taskId <br>* @param joinPoint <br>* @param exception <br>*/@AfterThrowing(value = "pointCut()", throwing = "exception")public void doAfterThrowingAdvice(JoinPoint joinPoint, Throwable exception) {Signature signature = joinPoint.getSignature();ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = sra.getRequest();log.info("[MainLogAspect]-response exception url:{}, class: {}, method: {}", request.getRequestURI(), signature.getDeclaringTypeName(), signature.getName());// MDC容器移除requestIdMDC.remove("requestId");}
}

MainLogAspect.java是自定义切面,拦截@Logc的请求,输出请求的入参出参,详细介绍可以戳:【系统日志笔记二】——撸起袖子写个自定义日志注解
在方法中合适的位置添加MDC.put和MDC.remove

  • 运行结果

    可以看到,日志中同一线程内的用户操作都会打印出一个requestId,如果线程中没有requestId则会输出一个空字符串。红框内第三行是在当前线程中启用了子线程,此时子线程并未集成父线程中MDC容器,其中原因可以在下面的源码跟踪里找到。

MDC源码与原理

MDC的源码在slf4j-api包的org.slf4j路径下,源码:

package org.slf4j;import java.io.Closeable;
import java.util.Map;import org.slf4j.helpers.NOPMDCAdapter;
import org.slf4j.helpers.BasicMDCAdapter;
import org.slf4j.helpers.Util;
import org.slf4j.impl.StaticMDCBinder;
import org.slf4j.spi.MDCAdapter;public class MDC {static final String NULL_MDCA_URL = "http://www.slf4j.org/codes.html#null_MDCA";static final String NO_STATIC_MDC_BINDER_URL = "http://www.slf4j.org/codes.html#no_static_mdc_binder";static MDCAdapter mdcAdapter;/*** An adapter to remove the key when done.*/public static class MDCCloseable implements Closeable {private final String key;private MDCCloseable(String key) {this.key = key;}public void close() {MDC.remove(this.key);}}private MDC() {}/*** As of SLF4J version 1.7.14, StaticMDCBinder classes shipping in various bindings* come with a getSingleton() method. Previously only a public field called SINGLETON * was available.* * @return MDCAdapter* @throws NoClassDefFoundError in case no binding is available* @since 1.7.14*/private static MDCAdapter bwCompatibleGetMDCAdapterFromBinder() throws NoClassDefFoundError {try {return StaticMDCBinder.getSingleton().getMDCA();} catch (NoSuchMethodError nsme) {// binding is probably a version of SLF4J older than 1.7.14return StaticMDCBinder.SINGLETON.getMDCA();}}static {try {mdcAdapter = bwCompatibleGetMDCAdapterFromBinder();} catch (NoClassDefFoundError ncde) {mdcAdapter = new NOPMDCAdapter();String msg = ncde.getMessage();if (msg != null && msg.contains("StaticMDCBinder")) {Util.report("Failed to load class \"org.slf4j.impl.StaticMDCBinder\".");Util.report("Defaulting to no-operation MDCAdapter implementation.");Util.report("See " + NO_STATIC_MDC_BINDER_URL + " for further details.");} else {throw ncde;}} catch (Exception e) {// we should never get hereUtil.report("MDC binding unsuccessful.", e);}}/*** Put a diagnostic context value (the <code>val</code> parameter) as identified with the* <code>key</code> parameter into the current thread's diagnostic 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>* This method delegates all work to the MDC of the underlying logging system.** @param key non-null key * @param val value to put in the map* * @throws IllegalArgumentException*           in case the "key" parameter is null*/public static void put(String key, String val) throws IllegalArgumentException {if (key == null) {throw new IllegalArgumentException("key parameter cannot be null");}if (mdcAdapter == null) {throw new IllegalStateException("MDCAdapter cannot be null. See also " + NULL_MDCA_URL);}mdcAdapter.put(key, val);}/*** Put a diagnostic context value (the <code>val</code> parameter) as identified with the* <code>key</code> parameter into the current thread's diagnostic 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>* This method delegates all work to the MDC of the underlying logging system.* <p>* This method return a <code>Closeable</code> object who can remove <code>key</code> when* <code>close</code> is called.** <p>* Useful with Java 7 for example :* <code>*   try(MDC.MDCCloseable closeable = MDC.putCloseable(key, value)) {*     ....*   }* </code>** @param key non-null key* @param val value to put in the map* @return a <code>Closeable</code> who can remove <code>key</code> when <code>close</code>* is called.** @throws IllegalArgumentException*           in case the "key" parameter is null*/public static MDCCloseable putCloseable(String key, String val) throws IllegalArgumentException {put(key, val);return new MDCCloseable(key);}/*** Get the diagnostic context identified by the <code>key</code> parameter. The* <code>key</code> parameter cannot be null.* * <p>* This method delegates all work to the MDC of the underlying logging system.** @param key  * @return the string value identified by the <code>key</code> parameter.* @throws IllegalArgumentException*           in case the "key" parameter is null*/public static String get(String key) throws IllegalArgumentException {if (key == null) {throw new IllegalArgumentException("key parameter cannot be null");}if (mdcAdapter == null) {throw new IllegalStateException("MDCAdapter cannot be null. See also " + NULL_MDCA_URL);}return mdcAdapter.get(key);}/*** Remove the diagnostic context identified by the <code>key</code> parameter using* the underlying system's MDC implementation. The <code>key</code> parameter* cannot be null. This method does nothing if there is no previous value* associated with <code>key</code>.** @param key  * @throws IllegalArgumentException*           in case the "key" parameter is null*/public static void remove(String key) throws IllegalArgumentException {if (key == null) {throw new IllegalArgumentException("key parameter cannot be null");}if (mdcAdapter == null) {throw new IllegalStateException("MDCAdapter cannot be null. See also " + NULL_MDCA_URL);}mdcAdapter.remove(key);}/*** Clear all entries in the MDC of the underlying implementation.*/public static void clear() {if (mdcAdapter == null) {throw new IllegalStateException("MDCAdapter cannot be null. See also " + NULL_MDCA_URL);}mdcAdapter.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 static Map<String, String> getCopyOfContextMap() {if (mdcAdapter == null) {throw new IllegalStateException("MDCAdapter cannot be null. See also " + NULL_MDCA_URL);}return mdcAdapter.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 passed as* parameter must only contain keys and values of type String.* * @param contextMap*          must contain only keys and values of type String* @since 1.5.1*/public static void setContextMap(Map<String, String> contextMap) {if (mdcAdapter == null) {throw new IllegalStateException("MDCAdapter cannot be null. See also " + NULL_MDCA_URL);}mdcAdapter.setContextMap(contextMap);}/*** Returns the MDCAdapter instance currently in use.* * @return the MDcAdapter instance currently in use.* @since 1.4.2*/public static MDCAdapter getMDCAdapter() {return mdcAdapter;}}

可以看到MDC具体操作都是通过MDCAdapter.java接口来操作,静态块中初始化得到MDCAdapter实现类的实例。

  • MDCAdapter.java
public class NOPMDCAdapter implements MDCAdapter {public void clear() {}public String get(String key) {return null;}public void put(String key, String val) {}public void remove(String key) {}public Map<String, String> getCopyOfContextMap() {return null;}public void setContextMap(Map<String, String> contextMap) {// NOP}}

slf4j-api包中对MDCAdapter接口提供了两个实现类BasicMDCAdapter和NOPMDCAdapter。在对slf4j提供具体实现的依赖包时也会提供其他MDCAdapter的实现,比如logback中提供了LogbackMDCAdapter,log4j2中提供了Log4jMDCAdapter。上述例子中集成了log4j2,下面看下Log4jMDCAdapter(log4j-slf4j-impl.jar)的具体实现:

public class Log4jMDCAdapter implements MDCAdapter {@Overridepublic void put(final String key, final String val) {ThreadContext.put(key, val);}@Overridepublic String get(final String key) {return ThreadContext.get(key);}@Overridepublic void remove(final String key) {ThreadContext.remove(key);}@Overridepublic void clear() {ThreadContext.clearMap();}@Overridepublic Map<String, String> getCopyOfContextMap() {return ThreadContext.getContext();}@Override@SuppressWarnings("unchecked") // nothing we can do about this, restricted by SLF4J APIpublic void setContextMap(@SuppressWarnings("rawtypes") final Map map) {ThreadContext.clearMap();for (final Map.Entry<String, String> entry : ((Map<String, String>) map).entrySet()) {ThreadContext.put(entry.getKey(), entry.getValue());}}

Log4jMDCAdapter内容很简洁,对容器的存取使用了一个封装类ThreadContext。
ThreadContext源码片段:

...//省略代码若干
private static ThreadContextMap contextMap;
private static ThreadContextStack contextStack;
...//省略代码若干
static void init() {contextMap = null;final PropertiesUtil managerProps = PropertiesUtil.getProperties();disableAll = managerProps.getBooleanProperty(DISABLE_ALL);useStack = !(managerProps.getBooleanProperty(DISABLE_STACK) || disableAll);useMap = !(managerProps.getBooleanProperty(DISABLE_MAP) || disableAll);contextStack = new DefaultThreadContextStack(useStack);if (!useMap) {contextMap = new NoOpThreadContextMap();} else {contextMap = ThreadContextMapFactory.createThreadContextMap();}
}
...//省略代码若干
public static void put(final String key, final String value) {contextMap.put(key, value);}
public static String get(final String key) {return contextMap.get(key);}
...//省略代码若干

ThreadContext类中存取数据使用了另一个封装ThreadContextMap,而默认ThreadContextMap由ThreadContextMapFactory.createThreadContextMap初始化:

public static ThreadContextMap createThreadContextMap() {final PropertiesUtil managerProps = PropertiesUtil.getProperties();final String threadContextMapName = managerProps.getStringProperty(THREAD_CONTEXT_KEY);final ClassLoader cl = ProviderUtil.findClassLoader();ThreadContextMap result = null;if (threadContextMapName != null) {try {final Class<?> clazz = cl.loadClass(threadContextMapName);if (ThreadContextMap.class.isAssignableFrom(clazz)) {result = (ThreadContextMap) clazz.newInstance();}} catch (final ClassNotFoundException cnfe) {LOGGER.error("Unable to locate configured ThreadContextMap {}", threadContextMapName);} catch (final Exception ex) {LOGGER.error("Unable to create configured ThreadContextMap {}", threadContextMapName, ex);}}if (result == null && ProviderUtil.hasProviders()) {final String factoryClassName = LogManager.getFactory().getClass().getName();for (final Provider provider : ProviderUtil.getProviders()) {if (factoryClassName.equals(provider.getClassName())) {final Class<? extends ThreadContextMap> clazz = provider.loadThreadContextMap();if (clazz != null) {try {result = clazz.newInstance();break;} catch (final Exception e) {LOGGER.error("Unable to locate or load configured ThreadContextMap {}",provider.getThreadContextMap(), e);result = createDefaultThreadContextMap();}}}}}if (result == null) {result = createDefaultThreadContextMap();}return result;}private static ThreadContextMap createDefaultThreadContextMap() {if (Constants.ENABLE_THREADLOCALS) {if (PropertiesUtil.getProperties().getBooleanProperty(GC_FREE_THREAD_CONTEXT_KEY)) {return new GarbageFreeSortedArrayThreadContextMap();}return new CopyOnWriteSortedArrayThreadContextMap();}return new DefaultThreadContextMap(true);}

最后默认使用DefaultThreadContextMap。DefaultThreadContextMap片段:

public DefaultThreadContextMap(final boolean useMap) {this.useMap = useMap;this.localMap = createThreadLocalMap(useMap);}// LOG4J2-479: by default, use a plain ThreadLocal, only use InheritableThreadLocal if configured.// (This method is package protected for JUnit tests.)static ThreadLocal<Map<String, String>> createThreadLocalMap(final boolean isMapEnabled) {final PropertiesUtil managerProps = PropertiesUtil.getProperties();final boolean inheritable = managerProps.getBooleanProperty(INHERITABLE_MAP);if (inheritable) {return new InheritableThreadLocal<Map<String, String>>() {@Overrideprotected Map<String, String> childValue(final Map<String, String> parentValue) {return parentValue != null && isMapEnabled //? Collections.unmodifiableMap(new HashMap<>(parentValue)) //: null;}};}// if not inheritable, return plain ThreadLocal with null as initial valuereturn new ThreadLocal<>();}

到这里已经很清楚的看到容器默认使用的是ThreadLocal<Map<String, String>>存储。所以在新建子线程时候需要通过MDC.getCopyOfContextMap()和MDC.setContextMap()将父线程MDC容器内容传递到子线程中。当然源码中注释提到,可以通过配置使用InheritableThreadLocal,这样子线程可以直接继承父线程中MDC容器内容,但是因为性能问题,默认是不启用InheritableThreadLocal的。
子线程示例:


可以看到子线程也打印出了父线程中设置的requestId值。
跟踪上面贴出来的源码,启用InheritableThreadLocal需要我们在工程里增加配置文件log4j2.component.properties,log4j-api.jar在加载时会通过PropertiesUtil读取该文件内配置 的属性。
log4j2.component.properties

# 启用InheritableThreadLocal
isThreadContextMapInheritable = false

至此代码内无需在手动操作MDC.getCopyOfContextMap()和MDC.setContextMap()了,子线程会继承父线程内已经设置的MDC内容。

对于想要自定义ThreadContextMap的需要,也可以在配置文件log4j2.component.propertie中指定自己的实现类。
ThreadContextMapFactory.java

public static ThreadContextMap createThreadContextMap() {final PropertiesUtil managerProps = PropertiesUtil.getProperties();final String threadContextMapName = managerProps.getStringProperty(THREAD_CONTEXT_KEY);final ClassLoader cl = ProviderUtil.findClassLoader();ThreadContextMap result = null;// 在log4j2.component.propertie文件配置log4j2.threadContextMap = xx.xxx.xxx.yourThreadContextMap,会使用自定义ThreadContextMap实体if (threadContextMapName != null) {try {final Class<?> clazz = cl.loadClass(threadContextMapName);if (ThreadContextMap.class.isAssignableFrom(clazz)) {result = (ThreadContextMap) clazz.newInstance();}} catch (final ClassNotFoundException cnfe) {LOGGER.error("Unable to locate configured ThreadContextMap {}", threadContextMapName);} catch (final Exception ex) {LOGGER.error("Unable to create configured ThreadContextMap {}", threadContextMapName, ex);}}// 此处会检查log4j-core.jar内META-INF/log4j-provider.properties文件是否指定了ThreadContextMap实现类if (result == null && ProviderUtil.hasProviders()) {final String factoryClassName = LogManager.getFactory().getClass().getName();for (final Provider provider : ProviderUtil.getProviders()) {if (factoryClassName.equals(provider.getClassName())) {final Class<? extends ThreadContextMap> clazz = provider.loadThreadContextMap();if (clazz != null) {try {result = clazz.newInstance();break;} catch (final Exception e) {LOGGER.error("Unable to locate or load configured ThreadContextMap {}",provider.getThreadContextMap(), e);result = createDefaultThreadContextMap();}}}}}// 使用默认ThreadContextMap实体if (result == null) {result = createDefaultThreadContextMap();}return result;}

Slf4j之MDC机制相关推荐

  1. slf4j的MDC机制

    MDC(Mapped Diagnostic Context)诊断上下文映射,是slf4j提供的一个支持动态打印日志信息的工具,举例来说,对于一个web Server,如果服务端想在打印出的每行日志中都 ...

  2. 基于SLF4J MDC机制实现日志的链路追踪

    一.打印HTTP request body和response body实现日志跟踪 request 的inputStream和response 的outputStream默认情况下是只能读一次, 不可 ...

  3. Slf4j MDC机制

    MDC 简介 MDC ( Mapped Diagnostic Contexts ),它是一个线程安全的存放诊断日志的容器. Logback设计的一个目标之一是对分布式应用系统的审计和调试.在现在的分布 ...

  4. MDC机制实现日志的链路追踪

    一.问题描述 排查问题用得最多的方式是查看日志,但是在现有系统中,各种无关日志穿行其中,导致没办法快速的找出用户在一次请求中所有的日志. 二.问题分析 我们没办法快速定位用户在一次请求中对应的所有日志 ...

  5. 切面是异步还是同步操作‘_分布式中采用Logback的MDC机制与AOP切面结合串联日志...

    导读:在实际开发中,打印日志是十分重要的.在生产环境中,如果日志打得好可以快速地排查问题,而在分布式的场景下,一个请求会跨越多个节点,既一个业务可能需要多个节点协调配合处理.那么日志将会分散,而为了更 ...

  6. SLF4J及其MDC详解

    SLF4J,即Simple Logging Facade for Java,是一个通用的.Java日志系统的抽象框架. 对于实现了SLF4J的日志系统,能够在应用部署时(而不是编译时)动态插入应用,使 ...

  7. 利用Slf4j的MDC跟踪方法调用链

    2019独角兽企业重金招聘Python工程师标准>>> why? 一个web项目通常提供很多URL访问地址, 项目一般都是分层处理,例如Controller-->Service ...

  8. logback利用mdc机制为日志增加traceId

    1 . 增加LogMdcFilter package cn.xbz.common.filter;import org.slf4j.MDC;import javax.servlet.*; import ...

  9. SLF4J(六) - MDC/MDCAdapter是什么?

    是什么? MDC(Mapped Diagnostic Context,映射调试上下文)是日志系统提供的一种方便在多线程条件下记录日志的功能 有什么用? 一个常用的场景就是Web服务器中给每个请求都给一 ...

最新文章

  1. 毛慧昀:决策树实现对鸢尾花数据集的分类
  2. 欢迎来到美多商城!-项目准备之项目介绍-项目需求分析-项目架构设计
  3. PHP __autoload函数(自动载入类文件)的使用方法
  4. 转 AIX7.2+11.2.0.4RAC实施
  5. hdu3986 spfa + 枚举最短路上的边
  6. Linux驱动学习笔记之并发控制
  7. adb logcat介绍
  8. 在windows、linux中开启nginx的Gzip压缩大大提高页面、图片加载速度转
  9. android 获取用户名和密码,如何通过Android中的电子邮件地址获取用户名和密码
  10. C/C++[codeup 1923]排序
  11. 无约束优化问题的相关讨论
  12. 一个简单小说阅读网页html,简单版小说搜索阅读(64位程序)
  13. JQuery文档分析1-JQuery核心与选择器
  14. VBA入门--VBA基础知识和简单技能技巧
  15. 基于QT和DCMTK的Dicom 图像浏览器---目录
  16. 高版本linux安装gamit,Ubuntu14下Gamit安装(示例代码)
  17. 18、弱电锦集:网络视频监控知识问答100例
  18. ffmpeg 中av_rescale_rnd 的含义
  19. 心态调整--缘起性空
  20. 去掉字符串头尾指定字符

热门文章

  1. 计算机内存坏了是什么反应,电脑内存损坏会引起哪些故障
  2. 团队形成的阶段_误区39:团队自然形成,不必费心打造
  3. iPhone开发网站、论坛和博客
  4. Android手机通过USB数据线共享Linux电脑网络
  5. 【媒体报道】2013eoe移动开发者大会圆满落幕
  6. 抖音跳转微信加好友功能实现解析
  7. c# 如何改变ToolStrip的大小,并向ToolStripButton加载合适大小的Image
  8. 写了一遍垃圾的CACTI安装使用参考
  9. 数据结构知识点概要(北大张铭版)
  10. goahead之GoAction实现