add logger patching (closes #21)

This commit is contained in:
Josh Junon 2017-03-28 14:12:14 -07:00
parent 3c51839251
commit f6f74b8ea7
4 changed files with 91 additions and 2 deletions

View File

@ -18,12 +18,14 @@ import inspect
import keyword
import linecache
import locale
import logging
import os
import re
import sys
import traceback
from better_exceptions.color import STREAM, SUPPORTS_COLOR
from better_exceptions.log import BetExcLogger, patch as patch_logging
from better_exceptions.repl import interact, get_repl
@ -293,7 +295,7 @@ def write_stream(data):
STREAM.buffer.write(data)
def excepthook(exc, value, tb):
def format_exception(exc, value, tb):
formatted, colored_source = format_traceback(tb)
if not str(value) and exc is AssertionError:
@ -301,12 +303,22 @@ def excepthook(exc, value, tb):
title = traceback.format_exception_only(exc, value)
full_trace = u'Traceback (most recent call last):\n{}{}\n'.format(formatted, title[0].strip())
write_stream(full_trace)
return full_trace
def excepthook(exc, value, tb):
formatted = format_exception(exc, value, tb)
write_stream(formatted)
sys.excepthook = excepthook
logging.setLoggerClass(BetExcLogger)
patch_logging()
if hasattr(sys, 'ps1'):
print('WARNING: better_exceptions will only inspect code from the command line\n'
' when using: `python -m better_exceptions\'. Otherwise, only code\n'

27
better_exceptions/log.py Normal file
View File

@ -0,0 +1,27 @@
from __future__ import absolute_import
import sys
from logging import Logger, StreamHandler
def patch():
import logging
from better_exceptions import format_exception
logging_format_exception = lambda exc_info: format_exception(*exc_info)
if hasattr(logging, '_defaultFormatter'):
logging._defaultFormatter.format_exception = logging_format_exception
patchables = [handler() for handler in logging._handlerList if isinstance(handler(), StreamHandler)]
patchables = [handler for handler in patchables if handler.stream == sys.stderr]
patchables = [handler for handler in patchables if handler.formatter is not None]
for handler in patchables:
handler.formatter.formatException = logging_format_exception
class BetExcLogger(Logger):
def __init__(self, *args, **kwargs):
super(BetExcLogger, self).__init__(*args, **kwargs)
patch()

49
test/test_logging.py Normal file
View File

@ -0,0 +1,49 @@
import better_exceptions
import logging
logging.basicConfig()
logger = logging.getLogger(__name__)
logging.raiseExceptions = True
qux = 15
def foo(cb):
qix = 20
try:
cb()
except:
logger.exception('callback failed')
def bar1():
baz = 80.5
logger.info('Hello')
def bar2():
baz = 890.50
logger.info('Hello', exc_info=True)
def bar3():
baz = 600.524
raise Exception('this is a test exception')
def bar4():
baz = 52
assert baz == 90
FNS = [
bar1,
bar2,
bar3,
bar4
]
for fn in FNS:
foo(fn)

View File

@ -37,6 +37,7 @@ function test_all {
test_case "./test/test_interactive.sh"
# test_case "./test/test_interactive_raw.sh"
test_case "./test/test_string.sh"
test_case "$BETEXC_PYTHON" "test/test_logging.py"
}
for encoding in ascii "UTF-8"; do