这几天,闲暇下来回头看了一下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和NDC类似,唯一区别就是MDC的每个栈值是一个map,可以在日志输出中,定义自己想输出什么信息:
MDC.put("first", "1111"); MDC.put("second", "222");
log4j配置:
-4r [%t] %-5p %c %x [%X{first}] - %m%n
就可以输出以first为key的信息.
看了一下源代码,NDC是使用hashtable来维护每个线程的栈,性能上有点损失的.