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.
TL;DR
Adds thread and async safe support of passing logging context into:
This enables clean notification about what is the context under which given logger message, or exception/traceback occurred.
This is not a breaking change. Existing code should just work as is, without any changes to output of the logger. The change will only affect the code that is under the scope of the context manager and/or decorator.
How to use
Decorator
meant for automatic adding function parameters to the logging context, in case parameters need to be skipped they just need to be decorated with
SkipLogging
annotation, and they wont be added to contextThreads
Threads
fromdatabricks.labs.blueprint.parallel
a has been patched to support logging context. Hence if functions being executed are decorated withlogging_context_params
, or are inside of thewith logging_context(...)
block, both logger, exceptions and tracebacks will be enriched.Implementation note: by default threads don't handle
ContextVars
inheritance from parent thread, onlyasyncio
is capable of doing this because it was part of ContextVar PEP. I've patched theThreads
code to do the same.Context manager
Context manager can be nested, the same applies to decorators. In case parameters would have overlaping names, behaviour is like with local variables, the most recent definition in scope is used.
Exceptions
Exception are carrying the context they occurred in, this feature is available in python 3.11+.
In case exception is bubbled up across many try / catch context scopes, only the original context is stored to provide the most meaningful information about origin of the exception
Version compatibility
PR State: