def test_to_dict(logger, active_handler): try: 1 / 0 except Exception: logger.exception() record = active_handler.records[0] exported = record.to_dict() record.close() imported = logbook.LogRecord.from_dict(exported) for key, value in iteritems(record.__dict__): if key[0] == '_': continue assert value == getattr(imported, key)
def to_dict(self, json_safe=False): """Exports the log record into a dictionary without the information that cannot be safely serialized like interpreter frames and tracebacks. """ self.pull_information() rv = {} for key, value in iteritems(self.__dict__): if key[:1] != '_' and key not in self._noned_on_close: rv[key] = value # the extra dict is exported as regular dict rv['extra'] = dict(rv['extra']) if json_safe: return to_safe_json(rv) return rv
def convert_record(self, old_record): """Converts a record from logbook to logging.""" if sys.version_info >= (2, 5): # make sure 2to3 does not screw this up optional_kwargs = {'func': getattr(old_record, 'func_name')} else: optional_kwargs = {} record = logging.LogRecord(old_record.channel, self.convert_level(old_record.level), old_record.filename, old_record.lineno, old_record.message, (), old_record.exc_info, **optional_kwargs) for key, value in iteritems(old_record.extra): record.__dict__.setdefault(key, value) record.created = self.convert_time(old_record.time) return record
def test_pickle(active_handler, logger): try: 1 / 0 except Exception: logger.exception() record = active_handler.records[0] record.pull_information() record.close() for p in xrange(pickle.HIGHEST_PROTOCOL): exported = pickle.dumps(record, p) imported = pickle.loads(exported) for key, value in iteritems(record.__dict__): if key[0] == '_': continue imported_value = getattr(imported, key) if isinstance(value, ZeroDivisionError): # in Python 3.2, ZeroDivisionError(x) != ZeroDivisionError(x) assert type(value) is type(imported_value) assert value.args == imported_value.args else: assert value == imported_value
WARNING = 4 NOTICE = 3 INFO = 2 DEBUG = 1 NOTSET = 0 _level_names = { CRITICAL: 'CRITICAL', ERROR: 'ERROR', WARNING: 'WARNING', NOTICE: 'NOTICE', INFO: 'INFO', DEBUG: 'DEBUG', NOTSET: 'NOTSET' } _reverse_level_names = dict((v, k) for (k, v) in iteritems(_level_names)) _missing = object() # on python 3 we can savely assume that frame filenames will be in # unicode, on Python 2 we have to apply a trick. if PY2: def _convert_frame_filename(fn): if isinstance(fn, unicode): fn = fn.decode(sys.getfilesystemencoding() or 'utf-8', 'replace') return fn else: def _convert_frame_filename(fn): return fn
def __init__(self, handlers, filter=None, bubble=False): Handler.__init__(self, NOTSET, filter, bubble) assert isinstance(handlers, dict) self._handlers = dict( (tag, isinstance(handler, Handler) and [handler] or handler) for (tag, handler) in iteritems(handlers))