Repository navigation
Use contextvars for the log context and add log_context - #52
Merged
Merged
Conversation
## Summary The global log context used `threading.local`, which does not follow `asyncio` tasks: concurrent tasks on one thread shared, and overwrote, each other's context. ## Changes - Store the context in a `contextvars.ContextVar`. Threads and tasks each get their own value. A new thread still starts empty, as before, and `set_log_context` now stores a copy of its arguments. - Add `log_context(**fields)`, a context manager that merges fields over the active context and restores the previous one on exit, including when the block raises. Blocks nest. - Export it from the package root, document it in the README and the changelog. - Tests: restoration, exceptions, nesting, records inside and outside the block, isolation between concurrent `asyncio` tasks, and the stored context being a copy. ## Verification - `pytest`: 111 passed (latest dependencies) and 93 passed with 18 skipped (no extras). `ruff` and `mypy --strict` are clean. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01U2nYHVGDdthQpcwNBPbp8r
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The global log context used
threading.local, which does not followasynciotasks: concurrent tasks on one thread shared, and overwrote, each other's context.Changes
contextvars.ContextVar. Threads and tasks each get their own value. A new thread still starts empty, as before, andset_log_contextnow stores a copy of its arguments.log_context(**fields), a context manager that merges fields over the active context and restores the previous one on exit, including when the block raises. Blocks nest.asynciotasks, and the stored context being a copy.Verification
pytest: 111 passed (latest dependencies) and 93 passed with 18 skipped (no extras).ruffandmypy --strictare clean.Closes #21
🤖 Generated with Claude Code
https://claude.ai/code/session_01U2nYHVGDdthQpcwNBPbp8r
Generated by Claude Code