Our order service puts the order number into every log line through the logging context, the MDC, so that one search shows everything that happened to an order. A servlet filter puts the number in when a request arrives and clears it when the response goes out. On the request thread that works perfectly.
In September we moved label printing and customer emails off the request path into a Spring thread pool, so checkout would not wait for them. Two weeks later a customer complained that her confirmation email had never arrived. Support searched for her order number and found the checkout and nothing after it. Searching for email errors in that minute found a failure logged against a different order, belonging to a customer who had received his email without any trouble.
Logback keeps the MDC in a ThreadLocal. It belongs to the thread that set it, and a pool thread does not inherit it from the thread that submitted the work.…