Skip to content

Commit cc6cd75

Browse files
committed
Update readme
1 parent aaca6ce commit cc6cd75

1 file changed

Lines changed: 31 additions & 34 deletions

File tree

README.md

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -108,40 +108,8 @@ except RuntimeError as exc:
108108
```
109109

110110

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...
113112
```python
114-
import logging
115-
116-
117-
## Setup
118-
import stackprinter
119-
120-
class VerboseExceptionFormatter(logging.Formatter):
121-
def formatException(self, exc_info):
122-
msg = stackprinter.format(exc_info)
123-
lines = msg.split('\n')
124-
lines_indented = ["" + line + "\n" for line in lines]
125-
msg_indented = "".join(lines_indented)
126-
return msg_indented
127-
128-
def configure_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-
145113
logger = logging.getLogger("some_logger")
146114

147115
try:
@@ -151,7 +119,7 @@ except:
151119
logger.exception('My hovercraft is full of eels.')
152120

153121
```
154-
Output:
122+
...but getting an annotated traceback in the resulting log, still.
155123
```
156124
2022-04-02 16:16:40,905 ERROR: My hovercraft is full of eels.
157125
┆ File "demo_logging.py", line 56, in <module>
@@ -173,6 +141,35 @@ Output:
173141
┆ TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
174142
```
175143

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.
145+
146+
```python
147+
# Logging setup
148+
import logging
149+
import stackprinter
150+
151+
class VerboseExceptionFormatter(logging.Formatter):
152+
def formatException(self, exc_info):
153+
msg = stackprinter.format(exc_info)
154+
lines = msg.split('\n')
155+
lines_indented = ["" + line + "\n" for line in lines]
156+
msg_indented = "".join(lines_indented)
157+
return msg_indented
158+
159+
def configure_logger(logger_name=None):
160+
fmt = '%(asctime)s %(levelname)s: %(message)s'
161+
formatter = VerboseExceptionFormatter(fmt)
162+
163+
164+
handler = logging.StreamHandler()
165+
handler.setFormatter(formatter)
166+
167+
logger = logging.getLogger(logger_name)
168+
logger.addHandler(handler)
169+
170+
configure_logger("some_logger")
171+
```
172+
See [demo_logging.py](https://github.com/cknd/stackprinter/blob/master/demo_logging.py) for a runnable example.
176173

177174
## Printing the current call stack
178175
To see your own thread's current call stack, call `show` or `format` anywhere outside of exception handling.

0 commit comments

Comments
 (0)