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
| 34 | |
| 35 | |
| 36 | class 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( |
no outgoing calls
searching dependent graphs…