MDC value missing from log

827 views Asked by At

My app is running on amazon SWF and I want to be able to see all the logs of a specific request using an slf4j MDC. For some code flows I see the MDC in the logs, but for some they are missing, for example: I have these 2 lines of code one after the other:

MDC.put(Constants.MDC_TRACE_ID, activityInput.getWorkflowId());
log.info("Starting workflow: {}", activityInput);

I See the log, and I see the activity input has a value in the workflow id, but the log is missing the MDC, and the rest of the logs in this code path are all without the MDC.

I don't think my log template or MDC configuration are the issue, because on most cases I do see the MDC in the logs, just here it's missing.

2

There are 2 answers

0
T.G. On BEST ANSWER

The issue coming from 2 things I wasn't aware of:

  1. I changed the MDC key to use TraceId instead of the default RequestId and there is a framework we are using that uses the default RequestId in the background.
  2. I changed the log template, but different parts of the code uses different templates, and I missed one.

This code was still using the template with RequestId and when my code put a string in the TraceId key, it didn't change the value of the relevant key and the MDC was still using the value from the underlaying framework.

1
Maxim Fateev On

My guess is that SWF uses asynchronous code and MDC is not propagated from Task to Task.