You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+5-3Lines changed: 5 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -111,15 +111,17 @@ except RuntimeError as exc:
111
111
112
112
### Integration with the standard `logging` module
113
113
114
-
It's also possible to integrate this neatly with standard logging calls through a bit of extra plumbing. The goal is to use the built in `logging` module's error handling method without explicitly importing `stackprinter` at the site of the logging call...
114
+
Through a bit of extra plumbing you can log errors with normal `logging` methods, without having to import `stackprinter` at the site of the logging call. So you can continue to have nice and simple error handlers like this...
115
+
115
116
```python
117
+
logger = logging.getLogger()
116
118
try:
117
119
nothing = {}
118
120
dangerous_function(nothing.get("something"))
119
121
except:
120
122
logger.exception('My hovercraft is full of eels.')
121
123
```
122
-
...but getting an annotated traceback in the resulting log, still.
124
+
...but still get annotated tracebacks in the resulting log.
123
125
```
124
126
2022-04-02 16:16:40,905 ERROR: My hovercraft is full of eels.
125
127
┆ File "demo_logging.py", line 56, in <module>
@@ -141,7 +143,7 @@ except:
141
143
┆ TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
142
144
```
143
145
144
-
You can achieve this by adding a [custom formatter](https://docs.python.org/3/howto/logging-cookbook.html#customized-exception-formatting) to the logger beforehand:
146
+
You get this by adding a [custom formatter](https://docs.python.org/3/howto/logging-cookbook.html#customized-exception-formatting) to the logger once before using it.
0 commit comments