【问题标题】:Spring AOP MDC Logging not working as expectedSpring AOP MDC 日志没有按预期工作
【发布时间】:2021-11-01 21:18:32
【问题描述】:

在我的代码中,我有 2 个方法 processInbound()processOutbound()。我正在尝试使用 AOP 加载 MDC 数据,以便在日志中我可以识别旅程。

我的代码正在运行,因为我可以在日志中看到所需的旅程详细信息。

以下是我的方法

public void processInboundData() {
  //Do get journey details in the following info
  log.info("In the method processInboundData");

  //Method i call to process the data
  //Do get journey details in all info's defined in  fetchAndSaveData method
  ddlDataService.fetchAndSaveData();

  //Don't get journey=Inbound in the following info
  log.info("After done");
}

这是方面

@Around("execution(*package.processInboundData(..))")
public Object processInboundData(final ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
  MDC.put(Journey , "Inbound");
  try {
     // invoke the method.
     return proceedingJoinPoint.proceed();
  } finally { 
     MDC.clear();
  }
}

我可以看到的问题是,在日志中,我确实在以下行中获得了具有正确旅程名称的日志行:

processInboundData - [journey=Inbound] - [] 方法中 处理入站数据

我还在被调用方法“fetchAndSaveData”的所有信息行中得到“[journey=Inbound]”

但我没有在方法调用返回后的行中获得“[journey=Inbound]”,因此日志行显示为缺少“[journey=Inbound]”。

processInboundData - - [] 完成后

不知道为什么会这样

任何帮助将不胜感激

【问题讨论】:

  • 您的日志是processInboundData - - [] After done,但在processInboundData() 方法内的代码中,您正在记录After method call。这是一个错字还是After done 记录在其他地方?
  • 抱歉,打错了,我已经更新了。
  • 谢谢 R.G,我已经检查过了,fetchAndSaveData 方法有 MDC.clear 清除上下文。 .相反,我使用 MDC.remove 来清除特定键。感谢所有做出贡献的人。

标签: spring-boot aop mdc


【解决方案1】:

感谢 R.G,最后被调用的方法正在执行 MDC.clear()。我不得不使用 MDC.remove 方法来删​​除方法 fetchAndSaveData() 中使用的特定键

【讨论】:

  • 这意味着你有另一个@Around 用于fetchAndSaveData()
猜你喜欢
  • 2017-12-29
  • 1970-01-01
  • 1970-01-01
  • 2015-05-19
  • 2016-10-25
  • 2021-09-03
  • 2014-02-22
  • 2019-11-27
  • 1970-01-01
相关资源
最近更新 更多