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

SMAR
  • 125
  • 1
  • 11
  • 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 Sep 03 '21 at 14:55
  • Apologies, that was a typo, i have updated it. – SMAR Sep 03 '21 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 Sep 03 '21 at 15:39

1 Answers1

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()

SMAR
  • 125
  • 1
  • 11