# Log correlation with Log4j2 and AsyncLoggerContextSelector

**URL:** <https://discuss.elastic.co/t/log-correlation-with-log4j2-and-asyncloggercontextselector/330646>\
**Category:** APM\
**Tags:** java\
**Created:** [April 24, 2023, 12:07pm UTC](https://discuss.elastic.co/t/log-correlation-with-log4j2-and-asyncloggercontextselector/330646 "2023-04-24T12:07:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelunik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kelunik/32/120140_2.png) [@kelunik](https://discuss.elastic.co/u/kelunik)\
**Post date:** [April 24, 2023, 12:07pm UTC](https://discuss.elastic.co/t/log-correlation-with-log4j2-and-asyncloggercontextselector/330646/1 "2023-04-24T12:07:22Z")

</div>

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

```java
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?

---

<div class="post-metadata">

**Author:** ![kelunik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kelunik/32/120140_2.png) [@kelunik](https://discuss.elastic.co/u/kelunik)\
**Post date:** [April 26, 2023, 8:15am UTC](https://discuss.elastic.co/t/log-correlation-with-log4j2-and-asyncloggercontextselector/330646/2 "2023-04-26T08:15:13Z")

</div>

These seem to be the relevant parts:

> <https://github.com/elastic/apm-agent-java/blob/058b2679b8fdfa2482fc2738090ca597fa6710a1/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/correlation/Log4j2LogCorrelationInstrumentation.java#L62>

[https://logging.apache.org/log4j/2.x/manual/extending.html#LogEventFactory](https://logging.apache.org/log4j/2.x/manual/extending.html#LogEventFactory)

> Note: When log4j is configured to have [all loggers asynchronous](https://logging.apache.org/log4j/2.x/manual/async.html#AllAsync), 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.

---

<div class="post-metadata">

**Author:** ![Jack\_Shirazi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_shirazi/32/91641_2.png) [@Jack\_Shirazi](https://discuss.elastic.co/u/Jack_Shirazi)\
**Post date:** [April 26, 2023, 1:56pm UTC](https://discuss.elastic.co/t/log-correlation-with-log4j2-and-asyncloggercontextselector/330646/3 "2023-04-26T13:56:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kelunik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kelunik/32/120140_2.png) [@kelunik](https://discuss.elastic.co/u/kelunik)\
**Post date:** [April 26, 2023, 2:15pm UTC](https://discuss.elastic.co/t/log-correlation-with-log4j2-and-asyncloggercontextselector/330646/4 "2023-04-26T14:15:11Z")

</div>

@Jack_Shirazi I've now added this to our already existing `ContextDataProvider`, see [Elastic APM · GitHub](https://gist.github.com/kelunik/c3971068a7d1a1f0acd7c2143028ce1b)

It seems like this might be a good API to use for the agent as well: [Log4j – Extending Log4j 2](https://logging.apache.org/log4j/2.x/manual/extending.html#custom-contextdataprovider)

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

---

<div class="post-metadata">

**Author:** ![Jack\_Shirazi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_shirazi/32/91641_2.png) [@Jack\_Shirazi](https://discuss.elastic.co/u/Jack_Shirazi)\
**Post date:** [May 3, 2023, 12:22pm UTC](https://discuss.elastic.co/t/log-correlation-with-log4j2-and-asyncloggercontextselector/330646/5 "2023-05-03T12:22:55Z")

</div>

Thanks for the report and the suggestions, we've opened [an issue](https://github.com/elastic/apm-agent-java/issues/3132) and hope to get to it next Q

---

<div class="post-metadata">

**Author:** ![kelunik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kelunik/32/120140_2.png) [@kelunik](https://discuss.elastic.co/u/kelunik)\
**Post date:** [May 3, 2023, 12:31pm UTC](https://discuss.elastic.co/t/log-correlation-with-log4j2-and-asyncloggercontextselector/330646/6 "2023-05-03T12:31:57Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 24, 2023, 8:32am UTC](https://discuss.elastic.co/t/log-correlation-with-log4j2-and-asyncloggercontextselector/330646/7 "2023-05-24T08:32:12Z")

</div>

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