0

I have a Spring MVC app that has logging set up using SLF4J. I am using two different aspects (using spring-aop) to do the logging, one to log all incoming http requests, and another to log asynchronous tasks. The asynchronous tasks are launched using an ExecutorService.

My understanding was that when you use an ExecutorService, the spawned thread's MDC will not inherit values already set. But it seems it does. Fair enough. I just call org.slf4j.MDC.clear() in my child thread to reset the values.

My problem is that values I am setting in the spawned child thread are also showing up in the original parent thread's MDC.

Is this normal, or am I doing something wrong? Is there any way to prevent this?

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Robert Bowen
  • 487
  • 2
  • 13
  • 24
  • Would you terribly mind sharing some code, ideally an [MCVE](http://stackoverflow.com/help/mcve)? This is StackOverflow. Thank you. Another question: Which logging framework do you use underneath SLF4J. Logback or Log4J? BTW, I fixed your typo, you wrote "sl4j" in the subject and in the body text. Now maybe the SLF4J experts find your question more easily. – kriegaex Apr 04 '18 at 01:46

1 Answers1

0

Sorry for not posting code. I thought my question was more of a general "is this supposed to be happening?" kind of question. But obviously, code always helps.

Fyi, I am using Log4j underneath SL4J.

At any rate, I found my problem. It has nothing to do really with MDC, and everything to do with the fact that in test mode, we are overriding the execute() method of the ThreadPoolTaskExecutor so that in fact it does not start a new Thread. I had no idea. Apparently this is being done because otherwise the tests have transactional problems. Which explains why I am having "data bleed" issues.

So it's my problem, not MDC's. I have confirmed that the HashTable backing up MDC is the same instance for both http requests and asynch tasks. So it looks like I'm stuck with it. :(

Anyway, thanks for responding.

Robert Bowen
  • 487
  • 2
  • 13
  • 24