Effective Logging in Java/JEE - Mapped Diagnostic Context | Java Code Geeks
What is MDC?
MDC stands for Mapped Diagnostic Context. It helps you to distinguish inter-leaving logs from multiple sources. Let me explain in detail. 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 grep(search) them in the log file, to make some sense of it. 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. MDC is thread-safe and uses a Map internally to store the context information.[Courtesy : Kalyan Dabburi]
How to use MDC?
a. Configure the information, which needs to be logged (user-id in this case) in the log4j.xml as part of ConversionPattern
Read full article from Effective Logging in Java/JEE - Mapped Diagnostic Context | Java Code Geeks
No comments:
Post a Comment