-
Notifications
You must be signed in to change notification settings - Fork 37
/
demo_logging_hack.py
51 lines (34 loc) · 1.3 KB
/
demo_logging_hack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import logging
import stackprinter
def patch_logging(**kwargs):
"""
Replace `formatException` on every log handler / formatter we can find
**kwargs are those of stackprinter.format
"""
# this is based on https://github.com/Qix-/better-exceptions/blob/master/better_exceptions/log.py
def format_exc(exc_info):
msg = stackprinter.format(exc_info, **kwargs)
msg_indented = ' ' + '\n '.join(msg.split('\n')).strip()
return msg_indented
if hasattr(logging, '_defaultFormatter'):
logging._defaultFormatter.formatException = format_exc
handlers = [handler_ref() for handler_ref in logging._handlerList]
is_patchable = lambda handler: handler.formatter is not None
patchable_handlers = filter(is_patchable, handlers)
for hd in patchable_handlers:
hd.formatter.formatException = format_exc
# option A: use the root loger:
logger = logging.getLogger()
# # option B: use a custom one:
# logging.basicConfig()
# logger = logging.getLogger('some logger')
patch_logging(style='darkbg')
#### test:
def dangerous_function(blub):
return sorted(blub, key=lambda xs: sum(xs))
try:
somelist = [[1,2], [3,4]]
anotherlist = [['5', 6]]
dangerous_function(somelist + anotherlist)
except:
logger.exception('the front fell off.')