def __init__(self, *args, regular_attrs=None, extra_attrs=[], **kwargs): """ :param list extra_attrs: String names of extra attributes that may exist on the log record. """ super().__init__(*args, **kwargs) self._dict_formatter = DictFormatter(regular_attrs=regular_attrs, extra_attrs=extra_attrs)
def test_format_ordered_2(self): record = util.random_log_record() formatter = DictFormatter(preserve_order=True, specific_order=["thread"]) dict_result = formatter.format(record) self.assertEqual("thread", dict_result.popitem(last=False)[0]) self.assertEqual(bool(len(dict_result) > 0), True)
def test_format_1(self, extra): record = util.random_log_record(extra=extra) formatter = DictFormatter(extra_attrs=list(extra.keys())) dict_result = formatter.format(record) for key in util.regular_attrs: assert key in dict_result, "Result missing regular arg key '{0}'".format(key) for key in extra: assert key in dict_result, "Result missing extra arg key '{0}'".format(key)
def test_format_ordered_1(self): extra = {fauxfactory.gen_string("alphanumeric", random.randint(1,30)):fauxfactory.gen_string( "alphanumeric", random.randint(1,30)) for x in range(0, random.randint(4,8))} expected_order = sorted(list(extra.keys()), key=str.lower) record = util.random_log_record(extra=extra) formatter = DictFormatter(regular_attrs=["message"], preserve_order=True) dict_result = formatter.format(record) self.assertEqual("message", dict_result.popitem(last=False)[0]) for key in expected_order: self.assertEqual(key, dict_result.popitem(last=False)[0])
def test_format_1(self, extra): record = util.random_log_record(extra=extra) formatter = DictFormatter(extra_attrs=list(extra.keys())) dict_result = formatter.format(record) for key in util.regular_attrs: assert key in dict_result, "Result missing regular arg key '{0}'".format( key) for key in extra: assert key in dict_result, "Result missing extra arg key '{0}'".format( key)
def test_format_ordered_1(self): extra = { fauxfactory.gen_string("alphanumeric", random.randint(1, 30)): fauxfactory.gen_string("alphanumeric", random.randint(1, 30)) for x in range(0, random.randint(4, 8)) } expected_order = sorted(list(extra.keys()), key=str.lower) record = util.random_log_record(extra=extra) formatter = DictFormatter(regular_attrs=["message"], preserve_order=True) dict_result = formatter.format(record) self.assertEqual("message", dict_result.popitem(last=False)[0]) for key in expected_order: self.assertEqual(key, dict_result.popitem(last=False)[0])
class DictHandler(logging.Handler): """Formats log records into a dict. Meant to be subclassed. This is just a convenience wrapper around :py:class:`driftwood.formatters.dict.DictFormatter`. """ def __init__(self, *args, regular_attrs=None, extra_attrs=[], **kwargs): """ :param list extra_attrs: String names of extra attributes that may exist on the log record. """ super().__init__(*args, **kwargs) self._dict_formatter = DictFormatter(regular_attrs=regular_attrs, extra_attrs=extra_attrs) def emit(self, record): """Super this in your subclass to format the record into a dict""" return self._dict_formatter.format(record)
def test_format_ordered_2(self): record = util.random_log_record() formatter = DictFormatter( preserve_order=True, specific_order=["thread"]) dict_result = formatter.format(record) self.assertEqual("thread", dict_result.popitem(last=False)[0]) self.assertEqual(bool(len(dict_result) > 0), True)