Logging with Request Correlation using MDC

Logging with Request Correlation using MDC
Note: We are hiring developers. Come join our freelance community.
1. Overview
In this tutorial, we’ll look at how we can use MDC to include a correlation id to the logs in a web application. This way it will be easier to trace the logs for a specific request.
2. MDC Details
Let’s first talk about some details on MDC.
Mapped Diagnostic Context or MDC manages contextual information on a per-thread basis. So we can benefit from MDC when logging in a multi-threaded application. For example, we can store the IP address of each request in a web application and insert this data into each log statement. In our case, we’ll introduce a unique correlation id for each request.
Web servers generally have a thread pool for handling requests. Consequently, the same thread is used for multiple different requests. So we must maintain the MDC to prevent data corruption. More specifically, after adding an entry to MDC, we must remove it when the request ends.
3. Sample Application
Let’s continue with our sample application.
Enlighter JS Syntax Highlighter
HelloController has a single endpoint and uses the HelloService class.
Enlighter JS Syntax Highlighter
HelloService is a simple service with the sayHello method.
4. MDC Filter
Firstly, we’ll create an MDC filter to generate and store a correlation id. Moreover, this id must be unique for each request:
Enlighter JS Syntax Highlighter
In MDCFilter, we’re putting the CorrelationId entry before the filterChain.doFilter call. By this way, all subsequent logging activities will include this correlation id. After the request is handled, we’re removing CorrelationId in the finally block. Note that the remove operation will run even if an exception occurs.
Now that we have MDC ready, we must configure the log format to include the correlation id.
Spring Boot enables us to define the log format in application.properties:
Enlighter JS Syntax Highlighter
The %X{CorrelationId} expression adds the correlation id to the log output.
When we look at the application logs, we can identify the statements belonging to a specific request.
Enlighter JS Syntax Highlighter
5. MDC HandlerInterceptor
Now we’ll look at how we can achieve similar behavior with HandlerInterceptor.
In the previous Filter implementation, we added the correlation id before DispatcherServlet handles the request. In the case of a HandlerInterceptor-based solution, we’ll add the correlation id inside the DispatcherServlet processing flow.
Enlighter JS Syntax Highlighter
Here, we have the MDCInterceptor class where we add CorrelationId in preHandle and remove it in afterCompletion.
We must also register this interceptor:
Enlighter JS Syntax Highlighter
When the application serves a request, it generates the correlation id:
Enlighter JS Syntax Highlighter
6. Summary
In this tutorial, we’ve looked at how we can correlate logs for a request using MDC. Check out the source code for all examples over on Github. The original article - here.
Isa Olmez, Senior Software Engineer @Upstack.co
"I am a skilled Backend Engineer with 12+ years of industry experience and know-how in the use of Java and Spring stack for the delivery of high-quality business applications on projects for clients using Spring Boot, MVC, JPA, JMS, and Security. I have team experience in both small and multinational companies working extensively on system design and software design patterns."