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)和 TraceID 来实现日志链(Log Chain)。MDC 是一种机制,用于在多个日志记录之间传递信息。TraceID 是一个唯一的标识符,用于标识一条日志记录。...
MemoryMappedFile 使用 C# 内存中分配一大块地址. 前边分配一个用与共同配置用 后边N个结构体 for循环建 一般用到此问题,两本程序 一个读一个写 循环读写 ----2015/09/24 Lyndon 上海----
基于.NET4.0的MemoryMappedFile实现共享内存通信 共享内存通信可以解决跨线程、跨进程、跨EXE之间通信的问题 对于需要传输图片等引用类数据,可以通过序列化和反序列化配合共享内存实现跨进程(跨EXE)通信 共享...
利用Memory Mapped File实践A,B跨程序的数据交换
NULL 博文链接:https://605113068.iteye.com/blog/1727088
MDB: A Memory-Mapped Database and Backend for OpenLDAP
uds诊断数据库Demo
前几天在网上下载一个struts2的helloword的例子,那个作者也真够缺德的,搞个错误的程序,害得我查了一天的程序错误。 最后发现竟然是struts.xml被写成啦sturts.xml。 碰见这样的问题先鄙视下提供例子的作者, ...
HTTP Status 404 - There is no Action mapped for namespace and action name BackMemberGroupAudit..doc
Memory-Mapped Files for Qualcomm Brew By Ray Rischpater
Mapped File Class (4KB)
mytabis 报错 Mapped Statements collection does not contain value for
$ npm i --save @nestjs/mapped-types 快速开始 构建功能时,在基本实体类型上构建变体通常很有用。 这种变体的一个很好的例子是数据传输对象(DTO)。 数据传输对象是用于封装数据并将其从应用程序的一部分发送到...
numpy numpy_numpy教程之MemorymappedFiles
Fixed comparing against disconnected mapped network drives to prompt for username/password and reconnect.
Life After BerkeleyDB- OpenLDAP's Memory-Mapped Database
MemoryMappedFiles4Java 描述 该库旨在将内存映射文件引入Java。 与已经存在的MappedByteBuffer相比,目标是更好地控制创建,修改和销毁。 它试图统一在不同操作系统上使用此类映射的接口,这意味着许多细节无法实现...
资源来自pypi官网。 资源全名:mapped_config-2.34-py2-none-any.whl
Mapped Diagnostic Contexts () 在每个日志消息中包含适当的上下文信息。 这些是可用于更改日志输出的属性(对于您的application.conf ): appName = MyApplication logger.json.dateformat = "yyyy-MM-dd HH:mm:ss...