Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/structlog/typing.py: 97%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# SPDX-License-Identifier: MIT OR Apache-2.0
2# This file is dual licensed under the terms of the Apache License, Version
3# 2.0, and the MIT License. See the LICENSE file in the root of this
4# repository for complete details.
6"""
7Type information used throughout *structlog*.
9For now, they are considered provisional. Especially `BindableLogger` will
10probably change to something more elegant.
12.. versionadded:: 22.2.0
13"""
15from __future__ import annotations
17import sys
19from types import TracebackType
20from typing import (
21 Any,
22 Callable,
23 Dict,
24 Mapping,
25 MutableMapping,
26 Optional,
27 Protocol,
28 TextIO,
29 Tuple,
30 Type,
31 Union,
32 runtime_checkable,
33)
36if sys.version_info >= (3, 11):
37 from typing import Self
38else:
39 from typing_extensions import Self
42WrappedLogger = Any
43"""
44A logger that is wrapped by a bound logger and is ultimately responsible for
45the output of the log entries.
47*structlog* makes *no* assumptions about it.
49.. versionadded:: 20.2.0
50"""
53Context = Union[Dict[str, Any], Dict[Any, Any]]
54"""
55A dict-like context carrier.
57.. versionadded:: 20.2.0
58"""
61EventDict = MutableMapping[str, Any]
62"""
63An event dictionary as it is passed into processors.
65It's created by copying the configured `Context` but doesn't need to support
66copy itself.
68.. versionadded:: 20.2.0
69"""
71ProcessorReturnValue = Union[
72 Mapping[str, Any], str, bytes, bytearray, Tuple[Any, ...]
73]
74"""
75A value returned by a processor.
76"""
78Processor = Callable[[WrappedLogger, str, EventDict], ProcessorReturnValue]
79"""
80A callable that is part of the processor chain.
82See :doc:`processors`.
84.. versionadded:: 20.2.0
85"""
87ExcInfo = Tuple[Type[BaseException], BaseException, Optional[TracebackType]]
88"""
89An exception info tuple as returned by `sys.exc_info`.
91.. versionadded:: 20.2.0
92"""
95ExceptionRenderer = Callable[[TextIO, ExcInfo], None]
96"""
97A callable that pretty-prints an `ExcInfo` into a file-like object.
99Used by `structlog.dev.ConsoleRenderer`.
101.. versionadded:: 21.2.0
102"""
105@runtime_checkable
106class ExceptionTransformer(Protocol):
107 """
108 **Protocol:** A callable that transforms an `ExcInfo` into another
109 datastructure.
111 The result should be something that your renderer can work with, e.g., a
112 ``str`` or a JSON-serializable ``dict``.
114 Used by `structlog.processors.format_exc_info()` and
115 `structlog.processors.ExceptionPrettyPrinter`.
117 Args:
118 exc_info: Is the exception tuple to format
120 Returns:
121 Anything that can be rendered by the last processor in your chain, for
122 example, a string or a JSON-serializable structure.
124 .. versionadded:: 22.1.0
125 """
127 def __call__(self, exc_info: ExcInfo) -> Any: ...
130@runtime_checkable
131class BindableLogger(Protocol):
132 """
133 **Protocol**: Methods shared among all bound loggers and that are relied on
134 by *structlog*.
136 .. versionadded:: 20.2.0
137 """
139 @property
140 def _context(self) -> Context: ...
142 def bind(self, **new_values: Any) -> Self: ...
144 def unbind(self, *keys: str) -> Self: ...
146 def try_unbind(self, *keys: str) -> Self: ...
148 def new(self, **new_values: Any) -> Self: ...
151class FilteringBoundLogger(BindableLogger, Protocol):
152 """
153 **Protocol**: A `BindableLogger` that filters by a level.
155 The only way to instantiate one is using `make_filtering_bound_logger`.
157 .. versionadded:: 20.2.0
158 .. versionadded:: 22.2.0 String interpolation using positional arguments.
159 .. versionadded:: 22.2.0
160 Async variants ``alog()``, ``adebug()``, ``ainfo()``, and so forth.
161 .. versionchanged:: 22.3.0
162 String interpolation is only attempted if positional arguments are
163 passed.
164 .. versionadded:: 25.5.0
165 String interpolation using dictionary-based arguments if the first and
166 only argument is a mapping.
168 """
170 def bind(self, **new_values: Any) -> FilteringBoundLogger:
171 """
172 Return a new logger with *new_values* added to the existing ones.
174 .. versionadded:: 22.1.0
175 """
177 def unbind(self, *keys: str) -> FilteringBoundLogger:
178 """
179 Return a new logger with *keys* removed from the context.
181 .. versionadded:: 22.1.0
182 """
184 def try_unbind(self, *keys: str) -> FilteringBoundLogger:
185 """
186 Like :meth:`unbind`, but best effort: missing keys are ignored.
188 .. versionadded:: 22.1.0
189 """
191 def new(self, **new_values: Any) -> FilteringBoundLogger:
192 """
193 Clear context and binds *initial_values* using `bind`.
195 .. versionadded:: 22.1.0
196 """
198 def is_enabled_for(self, level: int) -> bool:
199 """
200 Check whether the logger is enabled for *level*.
202 .. versionadded:: 25.1.0
203 """
205 def get_effective_level(self) -> int:
206 """
207 Return the effective level of the logger.
209 .. versionadded:: 25.1.0
210 """
212 def debug(self, event: str, *args: Any, **kw: Any) -> Any:
213 """
214 Log ``event % args`` with **kw** at **debug** level.
215 """
217 async def adebug(self, event: str, *args: Any, **kw: Any) -> Any:
218 """
219 Log ``event % args`` with **kw** at **debug** level.
221 ..versionadded:: 22.2.0
222 """
224 def info(self, event: str, *args: Any, **kw: Any) -> Any:
225 """
226 Log ``event % args`` with **kw** at **info** level.
227 """
229 async def ainfo(self, event: str, *args: Any, **kw: Any) -> Any:
230 """
231 Log ``event % args`` with **kw** at **info** level.
233 ..versionadded:: 22.2.0
234 """
236 def warning(self, event: str, *args: Any, **kw: Any) -> Any:
237 """
238 Log ``event % args`` with **kw** at **warn** level.
239 """
241 async def awarning(self, event: str, *args: Any, **kw: Any) -> Any:
242 """
243 Log ``event % args`` with **kw** at **warn** level.
245 ..versionadded:: 22.2.0
246 """
248 def warn(self, event: str, *args: Any, **kw: Any) -> Any:
249 """
250 Log ``event % args`` with **kw** at **warn** level.
251 """
253 async def awarn(self, event: str, *args: Any, **kw: Any) -> Any:
254 """
255 Log ``event % args`` with **kw** at **warn** level.
257 ..versionadded:: 22.2.0
258 """
260 def error(self, event: str, *args: Any, **kw: Any) -> Any:
261 """
262 Log ``event % args`` with **kw** at **error** level.
263 """
265 async def aerror(self, event: str, *args: Any, **kw: Any) -> Any:
266 """
267 Log ``event % args`` with **kw** at **error** level.
269 ..versionadded:: 22.2.0
270 """
272 def err(self, event: str, *args: Any, **kw: Any) -> Any:
273 """
274 Log ``event % args`` with **kw** at **error** level.
275 """
277 def fatal(self, event: str, *args: Any, **kw: Any) -> Any:
278 """
279 Log ``event % args`` with **kw** at **critical** level.
280 """
282 async def afatal(self, event: str, *args: Any, **kw: Any) -> Any:
283 """
284 Log ``event % args`` with **kw** at **critical** level.
286 ..versionadded:: 22.2.0
287 """
289 def exception(self, event: str, *args: Any, **kw: Any) -> Any:
290 """
291 Log ``event % args`` with **kw** at **error** level and ensure that
292 ``exc_info`` is set in the event dictionary.
293 """
295 async def aexception(self, event: str, *args: Any, **kw: Any) -> Any:
296 """
297 Log ``event % args`` with **kw** at **error** level and ensure that
298 ``exc_info`` is set in the event dictionary.
300 ..versionadded:: 22.2.0
301 """
303 def critical(self, event: str, *args: Any, **kw: Any) -> Any:
304 """
305 Log ``event % args`` with **kw** at **critical** level.
306 """
308 async def acritical(self, event: str, *args: Any, **kw: Any) -> Any:
309 """
310 Log ``event % args`` with **kw** at **critical** level.
312 ..versionadded:: 22.2.0
313 """
315 def msg(self, event: str, *args: Any, **kw: Any) -> Any:
316 """
317 Log ``event % args`` with **kw** at **info** level.
318 """
320 async def amsg(self, event: str, *args: Any, **kw: Any) -> Any:
321 """
322 Log ``event % args`` with **kw** at **info** level.
323 """
325 def log(self, level: int, event: str, *args: Any, **kw: Any) -> Any:
326 """
327 Log ``event % args`` with **kw** at *level*.
328 """
330 async def alog(self, level: int, event: str, *args: Any, **kw: Any) -> Any:
331 """
332 Log ``event % args`` with **kw** at *level*.
333 """