Mapped Diagnostic Context (MDC)
针对 MDC功能,目前只有logback 以及 log4j 支持。
相关文章:
- https://www.baeldung.com/mdc-in-log4j-2-logback
- http://logback.qos.ch/manual/mdc.html
- https://www.jianshu.com/p/3fa7e7726fbb
- https://ketao1989.github.io/2015/04/29/LogBack-Implemention-And-Slf4j-Mdc/
Let's now use the SLF4J's flavor of MDC. In this case, the syntax and semantics are the same as that in log4j:
import org.slf4j.MDC;
public class Slf4jRunnable implements Runnable {
private final Transaction tx;
public Slf4jRunnable(Transaction tx) {
this.tx = tx;
}
public void run() {
MDC.put("transaction.id", tx.getTransactionId());
MDC.put("transaction.owner", tx.getOwner());
new Slf4TransferService().transfer(tx.getAmount());
MDC.clear();
}
}
We have to provide the Logback configuration file, logback.xml:
<configuration>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%-4r [%t] %5p %c{1} - %m - tx.id=%X{transaction.id} tx.owner=%X{transaction.owner}%n</pattern>
</encoder>
</appender>
<root level="TRACE">
<appender-ref ref="stdout" />
</root>
</configuration>
Again, we'll see that the information in the MDC is properly added to the logged messages, even though this information is not explicitly provided in the log.info() method:
1020 [pool-1-thread-3] INFO c.b.m.s.Slf4jBusinessService
- Has transfer of 1869$ completed successfully ? true. - tx.id=3 tx.owner=John
1021 [pool-1-thread-3] INFO c.b.m.s.Slf4jBusinessService
- Preparing to transfer 1303$. - tx.id=6 tx.owner=Samantha
1221 [pool-1-thread-1] INFO c.b.m.s.Slf4jBusinessService
- Has transfer of 1498$ completed successfully ? true. - tx.id=4 tx.owner=Marc
1221 [pool-1-thread-1] INFO c.b.m.s.Slf4jBusinessService
- Preparing to transfer 1528$. - tx.id=7 tx.owner=Samantha
1492 [pool-1-thread-2] INFO c.b.m.s.Slf4jBusinessService
- Has transfer of 1110$ completed successfully ? true. - tx.id=5 tx.owner=Samantha
1493 [pool-1-thread-2] INFO c.b.m.s.Slf4jBusinessService
- Preparing to transfer 644$. - tx.id=8 tx.owner=John
不合理使用MDC的危害
MDC and Thread Pools
MDC implementations are usually using ThreadLocals to store the contextual information. That's an easy and reasonable way to achieve thread-safety. However, we should be careful using MDC with thread pools.
Let's see how the combination of ThreadLocal-based MDCs and thread pools can be dangerous:
- We get a thread from the thread pool.
- Then we store some contextual information in MDC using MDC.put() or ThreadContext.put().
- We use this information in some logs and somehow we forgot to clear the MDC context.
- The borrowed thread comes back to the thread pool.
- After a while, the application gets the same thread from the pool.
- Since we didn't clean up the MDC last time, this thread still owns some data from the previous execution.
This may cause some unexpected inconsistencies between executions. One way to prevent this is to always remember to clean up the MDC context at the end of each execution. This approach usually needs rigorous human supervision and, therefore, is error-prone.
相关推荐
MDC,全称Mapped Diagnostic Context,是SLF4J提供的一种上下文诊断功能,用于存储线程相关的诊断信息。在多线程环境中,MDC能够帮助我们跟踪和记录每个请求或事务的相关信息,这对于调试和监控非常有用。 MDC的...
本文将深入探讨一个名为"MDC-demo"的项目,该项目使用SpringMVC和MyBatis框架实现了基于MDC(Mapped Diagnostic Context)的日志跟踪打印功能。 首先,我们需要理解什么是MDC。MDC是Log4j框架中的一个特性,全称为...
8. **MDC和NDC**:Mapped Diagnostic Context (MDC) 和 Nested Diagnostic Context (NDC) 是Log4j提供的上下文信息存储功能,用于在日志信息中携带额外的调试信息,如请求ID、用户ID等。 总之,Log4j的配置连接是一...
6. **MDC(Mapped Diagnostic Context)**:SLF4J还提供了`org.slf4j.MDC`(Mapped Diagnostic Context),它是一个线程绑定的存储区域,可以用来存储与当前执行线程相关的诊断信息,如请求ID、用户ID等。...
本文将详细介绍如何利用Log4j的Nested Diagnostic Context (NDC) 和Mapped Diagnostic Context (MDC)特性来实现用户跟踪。 NDC和MDC是Log4j提供的两种上下文诊断工具,它们主要用于在多线程环境中添加额外的上下文...
Log4j的Mapped Diagnostic Context (MDC)可以辅助实现这一点。首先,为每个模块设置一个唯一的MDC键,然后在日志配置中根据这些键来设定级别。 5. **利用JMX(Java Management Extensions)** Log4j提供了一个JMX...
Monix-MDC,全称是Monix的Mapped Diagnostic Context,是一个专门为Monix Task设计的库,用于在异步执行环境中支持MDC(Mapped Diagnostic Context)。MDC是日志记录系统中一个常用的概念,它允许在多线程环境中传递...
此外,Log4j还支持MDC(Mapped Diagnostic Context)和NDC(Nested Diagnostic Context),这些特性可以附加上下文信息到日志记录中,对于调试多线程和分布式系统非常有用。 如果你对Log4j的具体使用还有疑问,可以...
在Spring Boot中,我们可以通过Logback的Mapped Diagnostic Context (MDC)特性来实现这一功能。 **MDC** 是Logback和log4j提供的一种工具,它允许我们在多线程环境中存储和检索诊断信息。当一个请求开始时,我们...
此外,它还可能会涵盖一些高级特性,如MDC(Mapped Diagnostic Context)和NDC(Nested Diagnostic Context)用于跟踪上下文信息,或者自定义日志输出策略。 学习和掌握Log4j不仅可以提高日志管理的效率,也有助于...
MDC(Mapped Diagnostic Context)是日志框架如Log4j、Logback中的一个概念,用于存储诊断信息,帮助开发者在多线程环境中追踪和调试日志。`spring-async-mdc`项目则关注如何在Spring的异步执行上下文中,正确地将...
8. **MDC和NDC**:Mapped Diagnostic Context (MDC) 和Nested Diagnostic Context (NDC) 提供了上下文信息,帮助追踪日志中的关键数据,例如线程ID、请求ID等。 9. **自定义日志记录器**:开发者可以根据需求创建...
8. **MDC和NDC**:Mapped Diagnostic Context (MDC) 和 Nested Diagnostic Context (NDC) 提供了上下文相关的日志信息,例如跟踪请求ID或用户ID。 9. **异常处理**:Log4Net可以方便地记录和处理异常信息,帮助...
8. **嵌入式诊断上下文(Nested Diagnostic Context, NDC)与线程诊断上下文(Mapped Diagnostic Context, MDC)** 这两个特性提供了一种方法来存储与当前执行路径相关的诊断信息,这对于多线程或多层架构的应用...
JBoss Logging支持`Mapped Diagnostic Context` (MDC) 和 `Nested Diagnostic Context` (NDC),这在多线程环境中尤其有用。源码中的`org.jboss.logging.MDC`和`org.jboss.logging.NDC`类提供了对应的API,允许...
9. **MDC和NDC**:Mapped Diagnostic Context (MDC) 和Nested Diagnostic Context (NDC) 提供了上下文信息,帮助跟踪和分析日志。MDC用于存储固定键值对,NDC则可以堆叠多个诊断信息。 10. **自定义Appender和...
10. **MDC与NDC**:Mapped Diagnostic Context (MDC) 和 Nested Diagnostic Context (NDC) 是Log4j提供的上下文信息管理工具,帮助关联特定的日志信息,便于调试和追踪。 通过研究Log4j 2.9.0的源码,开发者可以...
异常处理时,Mapped Diagnostic Context (MDC) 和 Nested Diagnostic Context (NDC) 提供了上下文信息,帮助追踪问题。MDC是键值对形式,NDC则是一个栈结构,方便记录调用堆栈信息。 8. **配置动态性**: 在运行...
2. **slf4j-ext**:这个模块扩展了SLF4J的基本功能,添加了一些特殊的功能,例如MDC(Mapped Diagnostic Context)、NDC(Nested Diagnostic Context)和异步日志记录。MDC用于存储与日志事件相关的键值对,便于过滤...
7. **MDC与NDC**:Mapped Diagnostic Context (MDC) 和 Nested Diagnostic Context (NDC) 提供了线程相关的上下文信息,帮助追踪和调试多线程程序。 8. **自定义日志记录**:Log4j允许用户自定义日志记录行为,例如...