Thursday, November 15, 2012

Using Log4j MDC (Mapped Diagnostic Context) in an Axis2 module

Mapped Diagnostic Context (MDC) is a thread-local key-value store that can be accessed from a Log4j conversion pattern. It can be used very effectively in web applications where one log serves several clients or users, to differentiate the log statements related to each client or request. In this example, the service a message is destined to, will be logged right next to the time of the log message, as the variable "service" in the MDC (referenced by %X). To make this work, you can put the Service name into the MDC, using the static method MDC.put(key, value).
MDC.put("service",messageContext.getAxisService().toString());
To use the MDC, you have to put a placeholder into the respective Log4j conversion pattern in lib/log4j.properties:
log4j.appender.CARBON_CONSOLE.layout.ConversionPattern=[%d{ISO8601}] [%X{service}] %5p - %c{1} - %m%n

No comments:

Post a Comment