Skip to content

Fix NoSuchMethodError in Log4j 2.7 log correlation - #4600

Open
Anshuman-Prakash wants to merge 1 commit into
elastic:mainfrom
Anshuman-Prakash:fix-log4j-2.7-correlation
Open

Anshuman-Prakash wants to merge 1 commit into
elastic:mainfrom
Anshuman-Prakash:fix-log4j-2.7-correlation

Conversation

@Anshuman-Prakash

Copy link
Copy Markdown

What does this PR do?

Fixes #4593

Log4J2_7PlusLogCorrelationInstrumentation is selected for log4j-core >= 2.7, but its helper cleaned up the correlation IDs with ThreadContext.removeAll(Iterable), which Log4j only added in 2.8. On 2.7 every logging event during a transaction therefore threw NoSuchMethodError from the advice. Because the advice suppresses it, the correlation keys were never removed, so a stale error.id leaked onto later log lines.

  • Log4j2_7PlusLogCorrelationHelper.removeFromMdc() now removes the keys one by one with ThreadContext.remove(String), as the 2.6 helper already does. putAll(Map) (available since 2.7) is still used to add them.
  • Log4j2InstrumentationTest now also runs the instrumentation and error-capture definitions against Log4j 2.7, next to 2.6, 2.17.1 and 2.24.0.

Tests

Run in maven:3.9-eclipse-temurin-17 (Linux):

mvn -pl apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin-tests -am install -DskipTests
mvn -pl apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin-tests test -Dtest=Log4j2InstrumentationTest
  • Without the fix (test change only): Tests run: 8, Failures: 1. testInstrumentation[2] (2.7) fails in verifyErrorCaptureAndCorrelation with expected: null but was: "16e9e1523a…" (a leaked error.id), and the log shows the NoSuchMethodError: ThreadContext.removeAll(java.lang.Iterable) from the issue.
  • With the fix: Tests run: 8, Failures: 0, Errors: 0, and there is no NoSuchMethodError in the output.

Checklist

  • This is an enhancement of existing features, or a new feature in existing plugins
  • This is a bugfix
  • This is a new plugin
  • This is something else

🤖 Generated with Claude Code

ThreadContext.removeAll(Iterable) is only available since Log4j 2.8, but
Log4j2_7PlusLogCorrelationHelper is selected for log4j-core >= 2.7. Remove
the correlation keys one by one with ThreadContext.remove(String) instead,
and run the log4j2 instrumentation tests against 2.7 as well.

Fixes elastic#4593

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Anshuman-Prakash
Anshuman-Prakash requested a review from a team as a code owner October 18, 2026 17:45
@github-actions

Copy link
Copy Markdown

🤖 GitHub comments

Just comment with:

  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)

@github-actions github-actions Bot added agent-java community Issues and PRs created by the community triage labels Oct 18, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-java community Issues and PRs created by the community triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Log4j 2.7 log correlation calls ThreadContext.removeAll, which was introduced in 2.8

1 participant