读书人

log4j中的NDC跟MDC

发布时间: 2012-12-23 11:28:15 作者: rapoo

log4j中的NDC和MDC

这几天,闲暇下来回头看了一下log4j,在看PatternLayout的各个字符的含义的时候,看见了这两个

x Used to output the NDC (nested diagnostic context) associated with the thread that generated the logging event. 
?
X Used to output the MDC (mapped diagnostic context) associated with the thread that generated the logging event. The X conversion character must be followed by the key for the map placed between braces, as in %X{clientNumber} where clientNumber is the key. The value in the MDC corresponding to the key will be output.See MDC class for more details. 
?

不懂啥叫NDC,啥叫MDC,google之,发现http://www.ibm.com/developerworks/cn/web/wa-lo-usertrack/

?

?

MDC.put("first", "1111"); MDC.put("second", "222");

?log4j配置:

-4r [%t] %-5p %c %x [%X{first}] - %m%n

?就可以输出以first为key的信息.

?

?

看了一下源代码,NDC是使用hashtable来维护每个线程的栈,性能上有点损失的.

?

?

读书人网 >编程

热点推荐