Questions tagged [mdc]

MDC (*mapped diagnostic context*) is a way to attach thread-scoped named identifier that can be added to every log message from that thread.

It is used to provide log messages with information that is unavailable to the scope when log is written.It provides the thread and user context to log messages and hence stamp each log for better tracking.

For example, it can help put Correlation Id to a request’s logs to help filter it out from similar logs at that time (from other quests) and also track it easily when this request hop from service to service by sharing the Correlation Id.

Typical use cases

Useful links

288 questions
194
votes
7 answers

How to use MDC with thread pools?

In our software we extensively use MDC to track things like session IDs and user names for web requests. This works fine while running in the original thread. However, there's a lot of things that need to be processed in the background. For that we…
Lóránt Pintér
  • 10,152
  • 14
  • 47
  • 53
26
votes
1 answer

Make logback pattern part optional?

Is it possible to make parts of logbacks pattern layout depending on an attribute? e.g. show bdid (...) just in the case when %X{bdid} exists? This appender
Stefan K.
  • 7,701
  • 6
  • 52
  • 64
20
votes
0 answers

Logback : using different patterns for different markers

The software I'm working on uses MDCs for specific data (username, ip, execution time and so on). I want some logs to use a specific pattern using some MDCs, and some logs to use another pattern using other MDCs. I thought that markers would do the…
Marie
  • 458
  • 2
  • 11
14
votes
4 answers

How to retain slf4j MDC logging context in CompletableFuture?

When executing async CompletableFuture, the parent threadcontext and moreover the org.slf4j.MDC context is lost. This is bad as I'm using some kind of "fish tagging" to track logs from one request among multiple logfiles. MDC.put("fishid",…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
13
votes
4 answers

How to use MDC with ForkJoinPool?

Following up on How to use MDC with thread pools? how can one use MDC with a ForkJoinPool? Specifically, I how can one wrap a ForkJoinTask so MDC values are set before executing a task?
Gili
  • 86,244
  • 97
  • 390
  • 689
13
votes
2 answers

logback print full MDC in appender

In an appender in a logback.xml is it possible to do any operation like iterating over an MDC, printing the entire MDC, etc.? The easiest way I can think to do this is basically via java/groovy code (which I haven't researched how to do yet), was…
djechlin
  • 59,258
  • 35
  • 162
  • 290
12
votes
4 answers

Preserve custom MDC attributes during exception-handling in Spring Boot

Short Version (With Enough Details) How to preserve the MDC attribute added in a doFilter() method of a javax.servlet.Filter implementation ... public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws…
Snackoverflow
  • 5,332
  • 7
  • 39
  • 69
12
votes
0 answers

How can I make the MDC available during logging in a spring controller advice?

I have a spring-boot web application which makes use of logback's MDC context to enrich the log with custom data. I have the following implementation in place which makes available some custom data associated with "customKey" and it properly gets…
jimonthebarn
  • 710
  • 9
  • 24
12
votes
2 answers

not able to put the values in the MDC

I am trying to log few values in the onBeginRequest() of RequestCycle() in wicket. But the values are not being logged in the debug file. I am putting the values in the MDC in RequestCycleListeners(). Following is the…
Ram Dutt Shukla
  • 1,351
  • 7
  • 28
  • 55
10
votes
2 answers

How to use a gRPC interceptor to attach/update logging MDC in a Spring-Boot app

Problem I have a Spring-Boot application in which I am also starting a gRPC server/service. Both the servlet and gRPC code send requests to a common object to process the request. When the request comes in I want to update the logging to display a…
Jim M.
  • 909
  • 1
  • 12
  • 29
10
votes
2 answers

How to use MDC with parallelStream in Java and logback

I need to log a few attributes of the request like the request id and the locale, but when using parallelStream, it seems that the ThreadLocal of the MDC looses the information. Ive analyzed the solution of passing the MDC context between the…
Guido Celada
  • 2,147
  • 1
  • 18
  • 23
10
votes
2 answers

How to use MDC of logback and SLF4J with spring boot to capture unique tracking in the POST request json?

We are using: Spring Boot Slf4J Logback ELK stack Now we want to use MDC to add the unique tracking number, which is provided in the POST request JSON, to every log statement for a given request. I googled some blog posts, those are not useful…
Bravo
  • 8,589
  • 14
  • 48
  • 85
10
votes
3 answers

Logging MDC with @Async and TaskDecorator

Using Spring MVC, I have the following setup: An AbstractRequestLoggingFilter derived filter for logging requests. A TaskDecorator to marshal the MDC context mapping from the web request thread to the @Async thread. I'm attempting to collect…
Ken
  • 741
  • 2
  • 9
  • 17
9
votes
1 answer

Add MDC variables in JsonLayout - log4j2

How to add MDC variables in the json log generated by JsonLayout of log4j2. I've used KeyValuePair tag to add properties like host name into the log, but I didn't found any way to add MDC variables into it. In pattern layout I used %X{traceId} but…
omjego
  • 373
  • 2
  • 5
  • 15
9
votes
1 answer

MDC(Mapped Diagnostic Context) support for JUL(Java.util.Logging)

I have a logging mechanism setup with Slf4j and java.util.Logging. I have several threads so I'm unable to get a clear idea from logs since they are mixed. Now I try to use MDC concept to add some more data with the log file so they are clear.…
Susitha Ravinda Senarath
  • 1,648
  • 2
  • 27
  • 49
1
2 3
19 20