Skip to content

Restore UNCACHED thread name updates in AsyncLogger - #4280

Open
AndrewBystrov wants to merge 2 commits into
apache:2.xfrom
AndrewBystrov:4275-uncached-thread-name
Open

AndrewBystrov wants to merge 2 commits into
apache:2.xfrom
AndrewBystrov:4275-uncached-thread-name

Conversation

@AndrewBystrov

Copy link
Copy Markdown

Fixes #4275.

Restore the current thread name for fully asynchronous AsyncLogger events when log4j2.asyncLoggerThreadNameStrategy is set to UNCACHED.

The thread-local translator now refreshes its thread values for the UNCACHED strategy before publishing each event. The CACHED strategy and the vararg translator behavior remain unchanged.

The regression test submits two tasks to a single-thread executor, renames the reused worker before each log call, and verifies that both log events contain their corresponding thread names. The test fails against the base revision and passes with this fix.

The test class was renamed from AsyncLoggerTestUncachedThreadName to AsyncLoggerUncachedThreadNameTest because the old name did not match Maven Surefire's default test discovery patterns. As a result, the existing test was not included in regular test runs, despite detecting the regression when executed explicitly.

Verification

  • The regression test passes in both configured Disruptor test executions
  • AsyncLoggerThreadNameStrategyTest: 6 tests pass in both executions
  • AsyncLogger test group: 82 tests, 0 failures, 1 skipped in both executions
  • ./mvnw verify -DskipTests -pl :log4j-core,:log4j-core-test
  • ./mvnw -N validate
  • git diff --check

Checklist

  • Changes are based on the 2.x branch
  • Relevant Maven tests and verification checks succeed
  • A changelog entry is included in src/changelog/.2.x.x
  • A regression test is provided

@ramanathan1504

Copy link
Copy Markdown
Contributor

@AndrewBystrov

Noted Give some time I will review and close.

Thanks for the Contribution

@AndrewBystrov

Copy link
Copy Markdown
Author

Hi @ramanathan1504, just a friendly follow-up on this PR. I understand reviews can take some time. Please let me know if there’s anything I can clarify or change on my side. Thanks!

@ramanathan1504

Copy link
Copy Markdown
Contributor

Hi @ramanathan1504, just a friendly follow-up on this PR. I understand reviews can take some time. Please let me know if there’s anything I can clarify or change on my side. Thanks!

Surely, I will try to close this weekend.👍

@ramanathan1504 ramanathan1504 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AndrewBystrov thanks, this puts back the UNCACHED refresh that #3171 removed, and the renamed test covers the reused worker thread from the issue.
Two small points are inline on AsyncLogger.java and one on the test. @ppkarwasz can you confirm whether RingBufferLogEventTranslator#updateThreadValues should stay deprecated now that it is called again?

}

private void initTranslatorThreadValues(final RingBufferLogEventTranslator translator) {
// constant check should be optimized out when using default (CACHED)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default strategy is UNCACHED on current JDKs, so this comment is wrong. Can we drop it?

Suggested change
// constant check should be optimized out when using default (CACHED)

private void initTranslatorThreadValues(final RingBufferLogEventTranslator translator) {
// constant check should be optimized out when using default (CACHED)
if (THREAD_NAME_CACHING_STRATEGY == ThreadNameCachingStrategy.UNCACHED) {
translator.updateThreadValues();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updateThreadValues() is still @Deprecated, and its Javadoc says the translator is only used on the thread that created it, which is the reason this call was removed. Should the deprecation go, or should the Javadoc mention Thread.setName()?

});
final String firstTaskThreadName = (String) firstTask.get();
final String secondTaskThreadName = (String) secondTask.get();
assertNotNull(firstTaskThreadName, "firstTaskThreadName");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each task returns a string literal, so these two assertNotNull calls cannot fail. Can we remove them?

@github-project-automation github-project-automation Bot moved this to Changes requested in Log4j pull request tracker Sep 14, 2026
@AndrewBystrov

Copy link
Copy Markdown
Author

Thanks for the feedback! I’ll address the requested changes. Regarding the updateThreadValues() deprecation and Javadoc, let’s wait for @ppkarwasz’s guidance before making any changes there.

@vy vy added the vy label Sep 16, 2026
@AndrewBystrov

Copy link
Copy Markdown
Author

Thanks for the feedback! I’ve addressed the requested changes in the latest commit. For the deprecation point, I kept updateThreadValues() deprecated and updated its Javadoc to clarify its internal use after Thread.setName(String)

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

Labels

Projects

Status: Changes requested

Development

Successfully merging this pull request may close these issues.

AsyncLogger ignores Thread.setName() changes despite the UNCACHED strategy since 2.25.0

3 participants