def test_multiline_value_bytes(self): try: 1 / 0 except: get_logger().exception('bad', diff=b'foo\nbar') compare(self.output.captured.splitlines()[:2], expected=[ "bad diff=b'foo\\nbar'", 'Traceback (most recent call last):', ])
def test_multiline_value_string(self): try: 1 / 0 except: get_logger().exception('bad', diff='foo\nbar') compare(self.output.captured.splitlines()[:5], expected=[ 'bad ', 'diff:', 'foo', 'bar', 'Traceback (most recent call last):', ])
def test_multiline_value_unicode_to_file(self): disk_file = NamedTemporaryFile(mode='ab+') handler = FileHandler(disk_file.name) handler.setFormatter(ShoehornFormatter()) self.logger.addHandler(handler) try: 1 / 0 except: get_logger().exception('bad', short='x', diff=u'foo\n\U0001F4A9') disk_file.seek(0) compare(disk_file.readlines()[:5], expected=[ b"bad short='x'\n", b'diff:\n', b'foo\n', b'\xf0\x9f\x92\xa9\n', b'Traceback (most recent call last):\n', ])
def test_extra_context(self): kw = OrderedDict([('exc_info', True), ('context', 'oh hai'), ('other', 1)]) if not PY2: kw['stack_info'] = True try: 1 / 0 except: logger = get_logger() if PY36: logger.info('foo %s', 'bar', **kw) else: logger.log_ordered('info', ('message', 'foo bar'), *kw.items()) compare(self.output.captured.splitlines()[0], expected="foo bar context='oh hai' other=1")
def test_named_logger(self): logger = get_logger('foo') logger.info(event='test') self.capture.check( ('foo', 'INFO', '', Event(event='test', logger='foo', level='info')))
from unittest import TestCase from testfixtures import LogCapture, compare from shoehorn import get_logger from shoehorn.compat import PY2 from shoehorn.event import Event logger = get_logger() class TestStandardLibraryTarget(TestCase): def setUp(self): self.capture = LogCapture(attributes=('name', 'levelname', 'getMessage', 'shoehorn_event')) self.addCleanup(self.capture.uninstall) def test_minimal(self): logger.info(event='test') self.capture.check( ('root', 'INFO', '', Event(event='test', level='info'))) def test_named_logger(self): logger = get_logger('foo') logger.info(event='test') self.capture.check( ('foo', 'INFO', '', Event(event='test', logger='foo', level='info'))) def test_level(self): logger.warning(event='test')
def test_no_message(self): get_logger().info(bar='foo') compare(self.output.captured.splitlines()[0], expected=" bar='foo'")
def test_multiline_message_interpolation(self): log = get_logger().bind(k='v') log.info('oh\n%s\nryl', 'ffs') compare(self.output.captured, expected="oh\nffs\nryl k='v'\n")
def test_bound_logger(self): self.handler.setFormatter( ShoehornFormatter('%(name)s %(message)s%(shoehorn_context)s')) get_logger('foo.bar').info('oh hai') compare(self.output.captured, expected="foo.bar oh hai\n")