Global context module
Module with routines to work with the global context.
This is the expected main entrypoint for users so let's try to keep it friendly to use and as simple as possible, but not simpler.
add_global_context(context, *, auto_init=True)
Add values to the global context to be attached to all the log messages.
The values will be removed from the global context once the context manager exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
dict[str, Any]
|
A key/value mapping with the values to add to the global context. |
required |
auto_init
|
bool
|
Indicate if the global context should be automatically initialized if it isn't. If If the global context is already initialized it'll do nothing. Keyword-only argument. |
True
|
Returns:
Type | Description |
---|---|
None
|
A context manager that manages the life of the values. |
Source code in src/logging_with_context/global_context.py
global_context_initialized(loggers=None)
Initiliaze the global context and manages its shutdown.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loggers
|
Optional[Sequence[Logger]]
|
The loggers to attach the global context; if not loggers are specified it will use the root logger. |
None
|
Returns:
Type | Description |
---|---|
None
|
A context manager with the global context initialized. |
Source code in src/logging_with_context/global_context.py
init_global_context(loggers=None)
Initialize the application global context in the given loggers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loggers
|
Optional[Sequence[Logger]]
|
The loggers to attach the global context; if not loggers are specified it will use the root logger. |
None
|
Source code in src/logging_with_context/global_context.py
shutdown_global_context(loggers=None)
Shutdown the application global context in the given loggers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loggers
|
Optional[Sequence[Logger]]
|
The loggers that were used when calling |
None
|