MCPcopy Create free account
hub / github.com/hynek/structlog / BytesLogger

Class BytesLogger

src/structlog/_output.py:250–351  ·  view source on GitHub ↗

r""" Writes bytes into a file. Useful if you follow `current logging best practices ` together with a formatter that returns bytes (e.g. `orjson `_). Args: file: File to print to. (default: `sys.stdout`\ ``.buffer``

Source from the content-addressed store, hash-verified

248
249
250class BytesLogger:
251 r"""
252 Writes bytes into a file.
253
254 Useful if you follow `current logging best practices
255 <logging-best-practices>` together with a formatter that returns bytes
256 (e.g. `orjson <https://github.com/ijl/orjson>`_).
257
258 Args:
259 file: File to print to. (default: `sys.stdout`\ ``.buffer``)
260
261 name:
262 Optional name for the logger. If provided, it will be picked up
263 as the logger&#x27;s name when used with
264 `structlog.stdlib.add_logger_name()` without using standard
265 library integration. ``BytesLogger`` itself does nothing with it.
266
267 .. versionadded:: 20.2.0
268
269 .. versionadded:: 26.1.0 The ``name`` attribute.
270 """
271
272 __slots__ = ("_file", "_flush", "_lock", "_write", "name")
273
274 def __init__(
275 self, file: BinaryIO | None = None, *, name: str | None = None
276 ):
277 self._file = file or sys.stdout.buffer
278 self._write = self._file.write
279 self._flush = self._file.flush
280
281 self.name = name
282
283 self._lock = _get_lock_for_file(self._file)
284
285 def __getstate__(self) -> tuple[str, str | None]:
286 """
287 Our __getattr__ magic makes this necessary.
288 """
289 if self._file is sys.stdout.buffer:
290 return "stdout", self.name
291
292 if self._file is sys.stderr.buffer:
293 return "stderr", self.name
294
295 raise PicklingError(
296 "Only BytesLoggers to sys.stdout and sys.stderr can be pickled."
297 )
298
299 def __setstate__(self, state: Any) -> None:
300 """
301 Our __getattr__ magic makes this necessary.
302 """
303 if isinstance(state, str):
304 name = None
305 else:
306 state, name = state
307

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…