简体   繁体   English

日志中缺少 MDC 值

[英]MDC value missing from log

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.我的应用程序在亚马逊 SWF 上运行,我希望能够使用 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,但对于某些代码流,它们丢失了,例如:我有以下 2 行代码:

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.我看到日志,我看到活动输入在工作流 ID 中有一个值,但是日志缺少 MDC,并且此代码路径中的日志的 rest 都没有 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.我认为我的日志模板或 MDC 配置不是问题,因为在大多数情况下,我确实在日志中看到了 MDC,只是在这里它不见了。

My guess is that SWF uses asynchronous code and MDC is not propagated from Task to Task.我的猜测是 SWF 使用异步代码并且 MDC 不会从一个任务传播到另一个任务。

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.我更改了 MDC 密钥以使用TraceId而不是默认的RequestId ,并且我们正在使用的框架在后台使用默认的RequestId
  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.此代码仍在使用带有RequestId的模板,当我的代码将字符串放入 TraceId 键时,它没有更改相关键的值,并且 MDC 仍在使用来自底层框架的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM