Log correlation with Log4j2 and AsyncLoggerContextSelector

If I use async logging, the transaction.id and trace.id fields aren't set in the log messages.

System.setProperty("log4j2.contextSelector", "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");

I'd like to keep using async logging, but still benefit from the trace and transaction correlation. What's the recommended way to do that?

These seem to be the relevant parts:

https://logging.apache.org/log4j/2.x/manual/extending.html#LogEventFactory

Note: When log4j is configured to have all loggers asynchronous, log events are pre-allocated in a ring buffer and the LogEventFactory is not used.

Elastic instruments the LogEventFactory, but this factory isn't used if async logging is used.

Yes that's the instrumentation applied for Log4j. I think you're right, I can't see where we would capture async logs. I'll discuss with the team next week but I don't think I have a useful answer for you now

@Jack_Shirazi I've now added this to our already existing ContextDataProvider, see Elastic APM · GitHub

It seems like this might be a good API to use for the agent as well: Log4j – Extending Log4j 2

I haven't anything for error.id, yet.

1 Like

Thanks for the report and the suggestions, we've opened an issue and hope to get to it next Q

Thanks!

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.