newrelic / newrelic/newrelic-java-agent
Log4j2 - Support the use of async appenders when using the standard Pattern Layout
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 240
- Forks
- 170
- Avg merge
- 3d 2h
- Merged PRs (30d)
- 21
Description
Original GTSE: https://new-relic.atlassian.net/jira/software/c/projects/NR/boards/289?selectedIssue=NR-323639
When using a Log4j async appender and the Pattern layout, trace id and span id are not properly added to the log event metadata because of a thread hop, which results in the active transaction no longer being available.
Module: https://github.com/newrelic/newrelic-java-agent/tree/main/instrumentation/apache-log4j-2.11
It might be possible to instrument a super class of the layout classes and instrument the toSerializable() method similarly to how the current AbstractJacksonLayout_Instrumentation is instrumented.
To enable async appenders in a project do the following:
- Create a file named
log4j2.component.propertiesin the same folder where thelog4j2.xmlfile exists - Add the following line to the file:
Log4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
An example log4j2.xml that configures a simple pattern layout with a console appender:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Appenders>
<Console name="console_log" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX},%-5p,%m%n" />
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="console_log" />
</Root>
</Loggers>
</Configuration>
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in instrumentation/apache-log4j-2.11 and read AbstractJacksonLayout_Instrumentation.java, focusing on how to instrument layout serialization. Investigate whether a superclass layout and its toSerializable() method can handle Pattern layouts across the async thread hop. Done means trace and span IDs are added to log event metadata when async appenders use the standard Pattern layout.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- observability-sre
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100