Search code examples
logginglog4jlog4j2mdc

log4j2 MDC data is not being printed for some loggers


After switching to log4j2 via the log4j-1.2-api bridge, I've noticed that MDC data is missing from some log lines. These lines come from a separate logger in the same class. What's puzzling is that this happens randomly – sometimes the MDC data appears, and other times it doesn't. Any ideas on why this inconsistency is happening?


Solution

  • If anybody gets here with the same problem this is because the log4j bridge uses ThreadContext in the background to redirect MDC calls and thread inheritance is not activated by default in the log4j2, thus to make it active you can add a log4j2.component.properties file and add this entry to it:

    log4j2.isThreadContextMapInheritable=true