Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

Add LoggingContext.withContext for safe ThreadContext management#97

Open
SpaceLeam wants to merge 3 commits into
apache:mainfrom
SpaceLeam:feature/scala-logging-context-cleanup
Open

Add LoggingContext.withContext for safe ThreadContext management#97
SpaceLeam wants to merge 3 commits into
apache:mainfrom
SpaceLeam:feature/scala-logging-context-cleanup

Conversation

@SpaceLeam

Copy link
Copy Markdown

Implements a functional wrapper (Loan Pattern) to ensure ThreadContext is automatically cleared after execution, preventing context leaks in asynchronous environments (e.g., Scala Futures).

@vy

vy commented Dec 26, 2025

Copy link
Copy Markdown
Member

@pjfanning, would you mind helping with the review of this PR, please?

@pjfanning

Copy link
Copy Markdown
Member

@vy the code seems ok to me - the only thing I would suggest is that the withContext methods could have @since info in their Scaladoc/Javadoc.

I guess the main problem is that the CI is busted because the action versions are out of date.

@vy vy self-assigned this Dec 26, 2025
@vy vy added the feature label Dec 26, 2025
@vy

vy commented Dec 26, 2025

Copy link
Copy Markdown
Member

@pjfanning, would you be interested in submitting a PR bumping the logging-parent and all other dependencies to the latest, and fixing the CI, please?

@vy

vy commented Dec 26, 2025

Copy link
Copy Markdown
Member

@SpaceLeam, could you implement the following changes, please?

  1. Add @since tags
  2. Briefly introduce the new API in src/site/antora/modules/ROOT/pages/index.adoc
  3. Add a changelog entry file to src/changelog/.13.x.x
  4. In the JavaDoc you added, use @link/@code tags for code (e.g., {@link ThreadContext})
  5. Make sure ./mvnw verify succeeds

@vy vy added this to the 13.2.0 milestone Dec 26, 2025
@pjfanning

Copy link
Copy Markdown
Member

@pjfanning, would you be interested in submitting a PR bumping the logging-parent and all other dependencies to the latest, and fixing the CI, please?

I'm afraid that I don't know much about the CI setup used by log4j team and I am not even really a user of log4j.

@SpaceLeam

SpaceLeam commented Dec 26, 2025

Copy link
Copy Markdown
Author

Thanks for the guidance, @vy. I see the discussion regarding the CI.

I'm on it. I will update the PR with the requested changes (JavaDocs, @SInCE tags, changelog entry, and documentation) and make sure the local build (./mvnw verify) passes. Will push the updates shortly.

@SpaceLeam SpaceLeam force-pushed the feature/scala-logging-context-cleanup branch from f908de2 to 41e9329 Compare December 26, 2025 17:39
@SpaceLeam

Copy link
Copy Markdown
Author
Screenshot_2025-12-26_12_42_35 Screenshot_2025-12-26_12_42_26

Status Update: Ready for Merge

@vy I have completed all the requested changes.

Summary of Changes:

  • Code: Implemented LoggingContext.withContext (Loan Pattern) across all modules.

  • Used scala.jdk.CollectionConverters for Scala 2.13+.

  • Used scala.collection.JavaConverters for Scala 2.10/2.11/2.12 to maintain backward compatibility.

  • Documentation:

  • Added @since 13.2.0 tags.

  • Fixed JavaDoc formatting (used {@link}).

  • Updated src/site/antora/modules/ROOT/pages/index.adoc with the usage example.

  • Changelog: Added the XML entry in src/changelog/.13.x.x linked to issue Add LoggingContext.withContext for safe ThreadContext management #97.

  • Testing: Added test("withContext") to LoggingContextTest.scala.

Verification:
I ran ./mvnw test locally to ensure stability (see screenshot below).

Passed: log4j-api-scala_2.12 and log4j-api-scala_2.13 compiled and passed all tests successfully.
Note: The CI checks are failing/pending due to infrastructure issues unrelated to these changes. My local build confirms the logic is sound.

The PR is updated and ready for review.


@SpaceLeam SpaceLeam force-pushed the feature/scala-logging-context-cleanup branch from aa4a160 to addeb37 Compare December 30, 2025 13:30
@SpaceLeam

Copy link
Copy Markdown
Author

Hi @vy, thanks for the catch!

I've updated the logic to properly backup and restore the ThreadContext values (instead of just removing them) to avoid state corruption. I also fixed the Javadoc links as requested.

Fixed in commit addeb37.

@vy

vy commented Dec 30, 2025

Copy link
Copy Markdown
Member

@SpaceLeam, would you also mind extending tests similar to CloseableThreadContextTest, please? See that there are several edge cases, and some burned our fingers in the past. Hence, feel free to copy the file as is and only change what is necessary to make it use the log4j-api-scala constructs.

@SpaceLeam

Copy link
Copy Markdown
Author

Hi @vy,

Just pushed the updates. I've added the comprehensive tests covering nesting, restoration, and exception handling (essentially ported the edge cases from CloseableThreadContextTest as suggested).

Verified the build locally and it's all green now (screenshot below).

Btw, I'm open to helping out with other issues in this repo if you need an extra hand. Just let me know.

Ready for review!
Screenshot_2025-12-30_10_45_40

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants