log(一)——MDC入门

 1.MDC是什么

先来一段原版注释,

/**
 * This class hides and serves as a substitute for the underlying logging
 * system's MDC implementation.
 * 
 * <p>
 * If the underlying logging system offers MDC functionality, then SLF4J's MDC,
 * i.e. this class, will delegate to the underlying system's MDC. Note that at
 * this time, only two logging systems, namely log4j and logback, offer MDC
 * functionality. For java.util.logging which does not support MDC,
 * {@link BasicMDCAdapter} will be used. For other systems, i.e slf4j-simple
 * and slf4j-nop, {@link NOPMDCAdapter} will be used.
 *
 * <p>
 * Thus, as a SLF4J user, you can take advantage of MDC in the presence of log4j,
 * logback, or java.util.logging, but without forcing these systems as
 * dependencies upon your users.
 * 
 * <p>
 * For more information on MDC please see the <a
 * href="http://logback.qos.ch/manual/mdc.html">chapter on MDC</a> in the
 * logback manual.
 * 
 * <p>
 * Please note that all methods in this class are static.
 * 
 * @author Ceki Gülcü
 * @since 1.4.1
 */

这是MDC类上的注释,但是没有讲这个是干什么的。具体的内容在有一个页面:

Chapter 8: Mapped Diagnostic Context

简单来说就是日志的增强功能,如果配置了MDC,并添加了相应的key value,就会在打日志的时候把key对应的value打印出来。

内部是用ThreadLocal来实现的,可以携带当前线程的context信息。

2.MDCAdapter——LogbackMDCAdapter

MDC中包装了一个MDCAdapter,这是一个接口,不同的log包有自己对应的实现。对于不支持MDC的包,比如java.util.logging,使用默认的BasicMDCAdapter,slf4j-nop有NOPMDCAdapter。

这里说的实现是LogbackMDCAdapter。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值