Solution

Luckily for us Log4j has a built in solution to this common problem: Nested Diagnostic Contexts.

To quote the API doc “A Nested Diagnostic Context, or NDC in short, is an instrument to distinguish interleaved log output from different sources. Log output is typically interleaved when a server handles multiple clients near-simultaneously.

Interleaved log output can still be meaningful if each log entry from different contexts had a distinctive stamp. This is where NDCs come into play….”