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
+31-34Lines changed: 31 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -108,40 +108,8 @@ except RuntimeError as exc:
108
108
```
109
109
110
110
111
-
It's also possible to integrate this neatly with standard logging calls [through a bit of extra plumbing](https://github.com/cknd/stackprinter/blob/master/demo_logging.py):
112
-
111
+
It's also possible to integrate this neatly with standard logging calls through a bit of extra plumbing. The goal is to use `logging` calls from the standard library without explicitly importing `stackprinter` at the site of the logging call...
lines_indented = [" ┆ "+ line +"\n"for line in lines]
125
-
msg_indented ="".join(lines_indented)
126
-
return msg_indented
127
-
128
-
defconfigure_logger(logger_name=None):
129
-
fmt ='%(asctime)s%(levelname)s: %(message)s'
130
-
formatter = VerboseExceptionFormatter(fmt)
131
-
132
-
133
-
handler = logging.StreamHandler()
134
-
handler.setFormatter(formatter)
135
-
136
-
logger = logging.getLogger(logger_name)
137
-
logger.addHandler(handler)
138
-
139
-
140
-
configure_logger("some_logger")
141
-
142
-
143
-
## Use
144
-
145
113
logger = logging.getLogger("some_logger")
146
114
147
115
try:
@@ -151,7 +119,7 @@ except:
151
119
logger.exception('My hovercraft is full of eels.')
152
120
153
121
```
154
-
Output:
122
+
...but getting an annotated traceback in the resulting log, still.
155
123
```
156
124
2022-04-02 16:16:40,905 ERROR: My hovercraft is full of eels.
157
125
┆ File "demo_logging.py", line 56, in <module>
@@ -173,6 +141,35 @@ Output:
173
141
┆ TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
174
142
```
175
143
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 before using it.
0 commit comments