0

In my code, I have 2 methods processInbound() and processOutbound(). I am trying to use AOP to load MDC data so that in the logs I can identify the journey.

My code is working as I can see the desired journey details in the log.

Following is my method

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");
}

This is the Aspect

@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();
  }
}

The problem that I can see is that in the logs I do get the log lines with the correct journey name in the following lines:

processInboundData - [journey=Inbound] - [] In the method processInboundData

I also get "[journey=Inbound]" in all the info lines that are there in the called method "fetchAndSaveData"

But I don't get "[journey=Inbound]" in the line just after the method call returns, so the logline shows up as with the missing "[journey=Inbound]".

processInboundData - - [] After done

Not sure why is this happening

Any help would be greatly appreciated

3
  • Your logline is processInboundData - - [] After done but in your code inside processInboundData() method you are logging After method call. Is this a typo or is After done logged somewhere else?
    – João Dias
    Commented Sep 3, 2021 at 14:55
  • Apologies, that was a typo, i have updated it.
    – SMAR
    Commented Sep 3, 2021 at 15:04
  • 1
    Thanks R.G , i have checked and the fetchAndSaveData method has MDC.clear which clears the context. . Instead i have used MDC.remove to clear the specific key. Thanks to all who contributed.
    – SMAR
    Commented Sep 3, 2021 at 15:39

1 Answer 1

0

Thanks to R.G, the called method at the end was doing MDC.clear(). I had to use MDC.remove method to remove the specific key used in the method fetchAndSaveData()

1
  • So that means you have another @Around for fetchAndSaveData()?
    – João Dias
    Commented Sep 3, 2021 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.