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

Class PrintLogger

src/structlog/_output.py:36–116  ·  view source on GitHub ↗

Print events into a file. Args: file: File to print to. (default: `sys.stdout`) >>> from structlog import PrintLogger >>> PrintLogger().info("hello") hello Useful if you follow `current logging best practices `. Also very useful fo

Source from the content-addressed store, hash-verified

34
35
36class PrintLogger:
37 """
38 Print events into a file.
39
40 Args:
41 file: File to print to. (default: `sys.stdout`)
42
43 >>> from structlog import PrintLogger
44 >>> PrintLogger().info("hello")
45 hello
46
47 Useful if you follow `current logging best practices
48 <logging-best-practices>`.
49
50 Also very useful for testing and examples since `logging` is finicky in
51 doctests.
52
53 .. versionchanged:: 22.1.0
54 The implementation has been switched to use `print` for better
55 monkeypatchability.
56 """
57
58 def __init__(self, file: TextIO | None = None):
59 self._file = file or stdout
60
61 self._lock = _get_lock_for_file(self._file)
62
63 def __getstate__(self) -> str:
64 """
65 Our __getattr__ magic makes this necessary.
66 """
67 if self._file is stdout:
68 return "stdout"
69
70 if self._file is stderr:
71 return "stderr"
72
73 raise PicklingError(
74 "Only PrintLoggers to sys.stdout and sys.stderr can be pickled."
75 )
76
77 def __setstate__(self, state: Any) -> None:
78 """
79 Our __getattr__ magic makes this necessary.
80 """
81 if state == "stdout":
82 self._file = stdout
83 else:
84 self._file = stderr
85
86 self._lock = _get_lock_for_file(self._file)
87
88 def __deepcopy__(self, memodict: dict[str, object]) -> PrintLogger:
89 """
90 Create a new PrintLogger with the same attributes. Similar to pickling.
91 """
92 if self._file not in (stdout, stderr):
93 raise copy.error(

Callers 2

__call__Method · 0.85

Calls

no outgoing calls

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…