The task in my software involves several steps where each step may or may not execute in different threads. Each step has some log and I use log4j as logging service.
A headache problem is it’s hard to associate logs printed by different threads to the task they belongs to. In single thread mode, the thread name is a good indicator, but it doesn’t work in multiple threads mode. I don’t want to pass a task indicator through threads as it’s annoying and will burden programmer.
I wonder anybody faced the same problem and had a good solution? thank you.
Look for log4j’s documentation on MDC (Mapped Diagnostic Context) and NDC (Nested Diagnostic Context). Below is the excerpt from this article