This commit is contained in:
@@ -9,8 +9,6 @@ Logger wrapper and helper class.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
|
||||
from typing import Any, Iterable, Mapping, Sequence
|
||||
|
||||
from structlog.exceptions import DropEvent
|
||||
@@ -18,12 +16,6 @@ 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.
|
||||
@@ -59,7 +51,9 @@ class BoundLoggerBase:
|
||||
self._context = context
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<{self.__class__.__name__}(context={self._context!r}, processors={self._processors!r})>"
|
||||
return "<{}(context={!r}, processors={!r})>".format(
|
||||
self.__class__.__name__, self._context, self._processors
|
||||
)
|
||||
|
||||
def __eq__(self, other: object) -> bool:
|
||||
try:
|
||||
@@ -70,7 +64,7 @@ class BoundLoggerBase:
|
||||
def __ne__(self, other: object) -> bool:
|
||||
return not self.__eq__(other)
|
||||
|
||||
def bind(self, **new_values: Any) -> Self:
|
||||
def bind(self, **new_values: Any) -> BoundLoggerBase:
|
||||
"""
|
||||
Return a new logger with *new_values* added to the existing ones.
|
||||
"""
|
||||
@@ -80,11 +74,12 @@ class BoundLoggerBase:
|
||||
self._context.__class__(self._context, **new_values),
|
||||
)
|
||||
|
||||
def unbind(self, *keys: str) -> Self:
|
||||
def unbind(self, *keys: str) -> BoundLoggerBase:
|
||||
"""
|
||||
Return a new logger with *keys* removed from the context.
|
||||
|
||||
Raises:
|
||||
|
||||
KeyError: If the key is not part of the context.
|
||||
"""
|
||||
bl = self.bind()
|
||||
@@ -93,7 +88,7 @@ class BoundLoggerBase:
|
||||
|
||||
return bl
|
||||
|
||||
def try_unbind(self, *keys: str) -> Self:
|
||||
def try_unbind(self, *keys: str) -> BoundLoggerBase:
|
||||
"""
|
||||
Like :meth:`unbind`, but best effort: missing keys are ignored.
|
||||
|
||||
@@ -105,13 +100,13 @@ class BoundLoggerBase:
|
||||
|
||||
return bl
|
||||
|
||||
def new(self, **new_values: Any) -> Self:
|
||||
def new(self, **new_values: Any) -> BoundLoggerBase:
|
||||
"""
|
||||
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 reused.
|
||||
are re-used.
|
||||
"""
|
||||
self._context.clear()
|
||||
|
||||
@@ -128,7 +123,8 @@ class BoundLoggerBase:
|
||||
Call it to combine your *event* and *context* into an event_dict and
|
||||
process using the processor chain.
|
||||
|
||||
Args:
|
||||
Arguments:
|
||||
|
||||
method_name:
|
||||
The name of the logger method. Is passed into the processors.
|
||||
|
||||
@@ -141,6 +137,7 @@ class BoundLoggerBase:
|
||||
*event_kw* ``{"bar": 42}``.
|
||||
|
||||
Raises:
|
||||
|
||||
structlog.DropEvent: if log entry should be dropped.
|
||||
|
||||
ValueError:
|
||||
@@ -151,6 +148,7 @@ class BoundLoggerBase:
|
||||
`tuple` of ``(*args, **kw)``
|
||||
|
||||
.. note::
|
||||
|
||||
Despite underscore available to custom wrapper classes.
|
||||
|
||||
See also `custom-wrappers`.
|
||||
@@ -178,7 +176,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
|
||||
return event_dict # type: ignore[return-value]
|
||||
|
||||
if isinstance(event_dict, dict):
|
||||
return (), event_dict
|
||||
@@ -199,7 +197,8 @@ class BoundLoggerBase:
|
||||
handling :exc:`structlog.DropEvent`, and finally calls *method_name* on
|
||||
:attr:`_logger` with the result.
|
||||
|
||||
Args:
|
||||
Arguments:
|
||||
|
||||
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
|
||||
@@ -214,6 +213,7 @@ class BoundLoggerBase:
|
||||
*event_kw* ``{"bar": 42}``.
|
||||
|
||||
.. note::
|
||||
|
||||
Despite underscore available to custom wrapper classes.
|
||||
|
||||
See also `custom-wrappers`.
|
||||
@@ -232,10 +232,12 @@ def get_context(bound_logger: BindableLogger) -> Context:
|
||||
The type of *bound_logger* and the type returned depend on your
|
||||
configuration.
|
||||
|
||||
Args:
|
||||
Arguments:
|
||||
|
||||
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