Improve Logging Using MDC

Fahim Fahad
2 min readJul 12, 2022

How to use MDC in spring boot and some best practices

We use logging in our projects to better understand the system. With proper logging, we can easily track down the issues. Using MDC (Mapped Diagnostic Context), we can improve logging. In this article, I will explain how we can use MDC and will provide a list of best practices for logging.

Photo by Jake Walker on Unsplash

Configure MDC

Now I will configure my spring boot application to use MDC for logging user specific data. I will use web filter to set user id in the MDC.

Normally, we can validate user authentication in the web filter. So we’ll have user data like id, session id, token validity, and so on. We must exercise caution when setting data into MDC. We should not store sensitive information there. In addition, once the request is complete, we must clear the data from MDC. Because MDC implementation uses ThreadLocal, it is possible that the same thread will serve other requests. If we do not clear MDC data after each execution, we may get an incorrect log.

Now we need to configure our log pattern (console and file) to use this data during logging.

I have added a logback-spring.xml file in the project. In the console and file log pattern I used a variable userId that I added in the MDC.

I uploaded the sample code in this branch: https://github.com/olein/Java-AWS-RnD/tree/MDC

All done. Now every log will print userId and we will be able to isolate each user log.

Logging best practices

  1. PII should not be logged.
  2. Log exceptions or errors with the proper log level
  3. Only log important events.
  4. Log short and precise message.
  5. Use timestamp with milliseconds in the log pattern. It will help identify concurrency cases.
  6. Do not concatenate strings for logging. Use this log.info("user {} accessing the resource", user.id);
  7. Do not log collection items. It will cause performance issues.
  8. Do not use AOP to log entry or exit. It will make life difficult to investigate the issues as we will have too many logs.
  9. If we perform expensive operations like toString(), computed results, and so on for debug log, we should check isDebugEnabled() before logging debug level logs.
  10. Configure the rolling appender
  11. Use a centralized log monitoring system.
  12. Try to use the same log pattern across different projects. It will help when reading the centralized log later.
  13. Add context to the log to better understand the scenario. Like MDC, class name, session id, etc.

--

--