Mapped Diagnostic Context (MDC)- Log files generation using Logback

Raveena Gupta
Deutsche Telekom Digital Labs
4 min readMay 31, 2024

What is Logback?

Logback serves the purpose of recording application messages, debugging details, and assorted outputs to multiple destinations, including the console, files, or remote servers. Logback offers support for various appender types, each of which determines where log messages are sent and stored. Multiple appenders can be assigned to a single Logger, and typically, appenders are associated with text files to store log information at their designated locations.

What is Mapped Diagnostic Context (MDC)?

The Mapped Diagnostic Context (MDC) is a functionality within the Logback framework, which serves as the default logging framework for Spring Boot. It enables you to retain contextual data within log messages. MDC functions as a key-value repository that is carried forward across different logging events. This permits you to save details like a user ID or request ID, which can then be incorporated into all log messages generated during a specific request.

Why MDC logging ?

MDC optimises the process of log analysis for more efficient issue diagnosis and system behaviour monitoring by making it easier to examine and link log entries related to a particular operation or user.

For example, if you were having problems with order creation, you could use MDC to store the order ID in each log message related to that order. This would make it easy to find all of the logs related to that order, and to see what happened to the order at each step of the process.

Here is another example, if you were debugging a performance problem, you could use MDC to store the request start time in each log message. This would make it easy to see how long each step of the process took, and to identify any bottlenecks.

MDC is a powerful tool that can help you to better understand your system and to troubleshoot problems more efficiently.

How to generate MDC log files using Logback ?

  1. We will start by adding logback dependency in your spring boot project:

2. After adding dependency we need to add logger in logback.xml configuration file to include MDC in log messages. In this example the key components include the “ORDER_DETAIL_FILE_APPENDER,” which sets up a specialized way to handle order log files using a tool called a “SiftingAppender.” A “discriminator” helps determine which log file to use based on the “MDC_BATCH_JOB_ID,” functioning like separate folders for different orders. Within the “sift” section, it details how each order’s log file is managed, with each order having its individual log file. It also specifies formatting for log messages and how older log files are handled.

3. In your application, you can use the MDC to store contextual information. For example, you can store the batch job Id(Unique Id) in the MDC:

4. Now, every log message emitted in the context of a single request will include the unique ID stored in the MDC. Here’s an example of log file, in this example we are using pattern :

%d{yyyy-MM-dd HH:mm:ss} %mdc [%thread] %level %logger{35} — %msg%n

This code formats a log message with the date and time, Mapped Diagnostic Context (additional context data), thread information, log level, logger name (limited to 35 characters), the actual message, and a newline character.


In conclusion, the above-described method enables us to generate personalised log files for individual users or requests, and the selection of appenders gives us authority over the length of log storage. Using MDC turns out to be a very effective method that improves system comprehension and makes troubleshooting procedures more effective.