- MDC stands for Mapped Diagnostic Context.
- It is a map which stores the context data of the particular thread where the context is running
Where do we use it ?
- For Jersey: ContainerRequestFilter, ContainerResponseFilter
- For Spring MVC : HttpServletRequest, HttpServletResponse
Why do we use it ?
- It helps you to distinguish inter-leaving logs from multiple sources.
- When we have multiple user-requests coming in for a given servlet, each request of an user is serviced using a thread. This leaves multiple users logging to the same log file and the log statements get inter-mixed.
- Now, to filter out logs of a particular user, we need to append the user-id to the log statements so that we can search them in the log file easily.
- An obvious way of logging, is to append the user-id in the log statements i.e. log.info(userId+” logged something “);
- A non-invasive way of logging is to use MDC.
- With MDC, you put the user-id in a context-map which is attached to the thread (of each user request) by the logger.
public class SimpleMDC {
static public void main(String[] args) throws Exception {
// You can put values in the MDC at any time. Before anything else
// we put the USER-ID
MDC.put("USER-ID", "DirecTV");
Logger logger = LoggerFactory.getLogger(SimpleMDC.class);
// We now put the IP-ADDRESS
MDC.put("IP-ADDRESS", "10.88.68.11");
logger.info("Start Logging");
logger.debug("Mapped Diagnostic Context");
logger.error("One of the design goals of logback is to audit and debug complex distributed applications");
logger.error("Most real-world distributed systems need to deal with multiple clients simultaneously. ");
logger.error("In a typical multithreaded implementation of such a system");
MDC.put("USER-ID", "FPT");
MDC.put("IP-ADDRESS", "100.888.688.111");
logger.info("Start Logging");
logger.debug("Mapped Diagnostic Context");
logger.error("One of the design goals of logback is to audit and debug complex distributed applications");
logger.error("Most real-world distributed systems need to deal with multiple clients simultaneously. ");
logger.error("In a typical multithreaded implementation of such a system");
}
}
static public void main(String[] args) throws Exception {
// You can put values in the MDC at any time. Before anything else
// we put the USER-ID
MDC.put("USER-ID", "DirecTV");
Logger logger = LoggerFactory.getLogger(SimpleMDC.class);
// We now put the IP-ADDRESS
MDC.put("IP-ADDRESS", "10.88.68.11");
logger.info("Start Logging");
logger.debug("Mapped Diagnostic Context");
logger.error("One of the design goals of logback is to audit and debug complex distributed applications");
logger.error("Most real-world distributed systems need to deal with multiple clients simultaneously. ");
logger.error("In a typical multithreaded implementation of such a system");
MDC.put("USER-ID", "FPT");
MDC.put("IP-ADDRESS", "100.888.688.111");
logger.info("Start Logging");
logger.debug("Mapped Diagnostic Context");
logger.error("One of the design goals of logback is to audit and debug complex distributed applications");
logger.error("Most real-world distributed systems need to deal with multiple clients simultaneously. ");
logger.error("In a typical multithreaded implementation of such a system");
}
}
Source Code
No comments:
Post a Comment