【发布时间】: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