This commit is contained in:
@@ -9,6 +9,8 @@ Logger wrapper and helper class.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
|
||||
from typing import Any, Iterable, Mapping, Sequence
|
||||
|
||||
from structlog.exceptions import DropEvent
|
||||
@@ -16,6 +18,12 @@ from structlog.exceptions import DropEvent
|
||||
from .typing import BindableLogger, Context, Processor, WrappedLogger
|
||||
|
||||
|
||||
if sys.version_info >= (3, 11):
|
||||
from typing import Self
|
||||
else:
|
||||
from typing_extensions import Self
|
||||
|
||||
|
||||
class BoundLoggerBase:
|
||||
"""
|
||||
Immutable context carrier.
|
||||
@@ -51,9 +59,7 @@ class BoundLoggerBase:
|
||||
self._context = context
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "<{}(context={!r}, processors={!r})>".format(
|
||||
self.__class__.__name__, self._context, self._processors
|
||||
)
|
||||
return f"<{self.__class__.__name__}(context={self._context!r}, processors={self._processors!r})>"
|
||||
|
||||
def __eq__(self, other: object) -> bool:
|
||||
try:
|
||||
@@ -64,7 +70,7 @@ class BoundLoggerBase:
|
||||
def __ne__(self, other: object) -> bool:
|
||||
return not self.__eq__(other)
|
||||
|
||||
def bind(self, **new_values: Any) -> BoundLoggerBase:
|
||||
def bind(self, **new_values: Any) -> Self:
|
||||
"""
|
||||
Return a new logger with *new_values* added to the existing ones.
|
||||
"""
|
||||
@@ -74,12 +80,11 @@ class BoundLoggerBase:
|
||||
self._context.__class__(self._context, **new_values),
|
||||
)
|
||||
|
||||
def unbind(self, *keys: str) -> BoundLoggerBase:
|
||||
def unbind(self, *keys: str) -> Self:
|
||||
"""
|
||||
Return a new logger with *keys* removed from the context.
|
||||
|
||||
Raises:
|
||||
|
||||
KeyError: If the key is not part of the context.
|
||||
"""
|
||||
bl = self.bind()
|
||||
@@ -88,7 +93,7 @@ class BoundLoggerBase:
|
||||
|
||||
return bl
|
||||
|
||||
def try_unbind(self, *keys: str) -> BoundLoggerBase:
|
||||
def try_unbind(self, *keys: str) -> Self:
|
||||
"""
|
||||
Like :meth:`unbind`, but best effort: missing keys are ignored.
|
||||
|
||||
@@ -100,13 +105,13 @@ class BoundLoggerBase:
|
||||
|
||||
return bl
|
||||
|
||||
def new(self, **new_values: Any) -> BoundLoggerBase:
|
||||
def new(self, **new_values: Any) -> Self:
|
||||
"""
|
||||
Clear context and binds *new_values* using `bind`.
|
||||
|
||||
Only necessary with dict implementations that keep global state like
|
||||
those wrapped by `structlog.threadlocal.wrap_dict` when threads
|
||||
are re-used.
|
||||
are reused.
|
||||
"""
|
||||
self._context.clear()
|
||||
|
||||
@@ -123,8 +128,7 @@ class BoundLoggerBase:
|
||||
Call it to combine your *event* and *context* into an event_dict and
|
||||
process using the processor chain.
|
||||
|
||||
Arguments:
|
||||
|
||||
Args:
|
||||
method_name:
|
||||
The name of the logger method. Is passed into the processors.
|
||||
|
||||
@@ -137,7 +141,6 @@ class BoundLoggerBase:
|
||||
*event_kw* ``{"bar": 42}``.
|
||||
|
||||
Raises:
|
||||
|
||||
structlog.DropEvent: if log entry should be dropped.
|
||||
|
||||
ValueError:
|
||||
@@ -148,7 +151,6 @@ class BoundLoggerBase:
|
||||
`tuple` of ``(*args, **kw)``
|
||||
|
||||
.. note::
|
||||
|
||||
Despite underscore available to custom wrapper classes.
|
||||
|
||||
See also `custom-wrappers`.
|
||||
@@ -176,7 +178,7 @@ class BoundLoggerBase:
|
||||
if isinstance(event_dict, tuple):
|
||||
# In this case we assume that the last processor returned a tuple
|
||||
# of ``(args, kwargs)`` and pass it right through.
|
||||
return event_dict # type: ignore[return-value]
|
||||
return event_dict
|
||||
|
||||
if isinstance(event_dict, dict):
|
||||
return (), event_dict
|
||||
@@ -197,8 +199,7 @@ class BoundLoggerBase:
|
||||
handling :exc:`structlog.DropEvent`, and finally calls *method_name* on
|
||||
:attr:`_logger` with the result.
|
||||
|
||||
Arguments:
|
||||
|
||||
Args:
|
||||
method_name:
|
||||
The name of the method that's going to get called. Technically
|
||||
it should be identical to the method the user called because it
|
||||
@@ -213,7 +214,6 @@ class BoundLoggerBase:
|
||||
*event_kw* ``{"bar": 42}``.
|
||||
|
||||
.. note::
|
||||
|
||||
Despite underscore available to custom wrapper classes.
|
||||
|
||||
See also `custom-wrappers`.
|
||||
@@ -232,12 +232,10 @@ def get_context(bound_logger: BindableLogger) -> Context:
|
||||
The type of *bound_logger* and the type returned depend on your
|
||||
configuration.
|
||||
|
||||
Arguments:
|
||||
|
||||
Args:
|
||||
bound_logger: The bound logger whose context you want.
|
||||
|
||||
Returns:
|
||||
|
||||
The *actual* context from *bound_logger*. It is *not* copied first.
|
||||
|
||||
.. versionadded:: 20.2.0
|
||||
|
||||
Reference in New Issue
Block a user