Slf4j logging context intermitently dissapears in http4s api

I’d be quite surprised if the MDC were reliable at all in this setup. The slf4j MDC is only an adapter for the underlying logging system’s MDC (if present at all). Most concrete MDC mechanisms will be implemented using ThreadLocal or similar, which won’t play well with IO fibers. I just experimented a little, e.g.

IO { MDC.put("foo", "bar") } >>
  List.fill(10)(IO { Option(MDC.get("foo")) }.flatMap(IO.println))
    .parSequence_

…and at least with logback as the underlying log system, seeing the entry at all seems to be a stroke of fortune.

The code you link to is about the log4cats logger pushing and retracting its context into/from the underlying system’s MDC around log actions at its own level - nothing to be inferred about lower level MDC behavior there.

You could switch to running in Kleisli[IO, CorrelationID, *] instead of IO and feed the context data into the (log4cats) logger context for each logging action. However, this will probably incur quite some refactorings and preparations - I have found these changes worthwhile, YMMV. See this post regarding a related logging issue to see what application code might look like with this approach.