def test_threaded_wrapper_handler_emit(): from logbook.queues import ThreadedWrapperHandler test_handler = BatchTestHandler() with ThreadedWrapperHandler(test_handler) as handler: lr = logbook.LogRecord('Test Logger', logbook.WARNING, 'Just testing') test_handler.emit(lr) lr = logbook.LogRecord('Test Logger', logbook.ERROR, 'More testing') test_handler.emit(lr) # give it some time to sync up handler.close() assert (not handler.controller.running) assert len(test_handler.records) == 2 assert len(test_handler.batches) == 2 assert all((len(records) == 1 for records in test_handler.batches)) assert test_handler.has_warning('Just testing') assert test_handler.has_error('More testing')
def make_record(self, message, exception, filename, lineno): category = exception.__name__ if exception.__module__ not in ('exceptions', 'builtins'): category = exception.__module__ + '.' + category rv = logbook.LogRecord(category, logbook.WARNING, message) # we don't know the caller, but we get that information from the # warning system. Just attach them. rv.filename = filename rv.lineno = lineno return rv
def convert_record(self, old_record): """Converts an old logging record into a logbook log record.""" record = logbook.LogRecord(old_record.name, self.convert_level(old_record.levelno), old_record.getMessage(), None, None, old_record.exc_info, self.find_extra(old_record), self.find_caller(old_record)) record.time = self.convert_time(old_record.created) return record
def test_threaded_wrapper_handler_emit_batched(): from logbook.queues import ThreadedWrapperHandler test_handler = BatchTestHandler() with ThreadedWrapperHandler(test_handler) as handler: test_handler.emit_batch([ logbook.LogRecord('Test Logger', logbook.WARNING, 'Just testing'), logbook.LogRecord('Test Logger', logbook.ERROR, 'More testing'), ], 'group') # give it some time to sync up handler.close() assert (not handler.controller.running) assert len(test_handler.records) == 2 assert len(test_handler.batches) == 1 (records, ) = test_handler.batches assert len(records) == 2 assert test_handler.has_warning('Just testing') assert test_handler.has_error('More testing')
def test_exception_catching_with_unicode(): """ See https://github.com/mitsuhiko/logbook/issues/104 """ try: raise Exception(u('\u202a test \u202c')) except: r = logbook.LogRecord('channel', 'DEBUG', 'test', exc_info=sys.exc_info()) r.exception_message
def fake_record(message, year, month, day, hour=0, minute=0, second=0): lr = logbook.LogRecord('Test Logger', logbook.WARNING, message) lr.time = datetime(year, month, day, hour, minute, second) return lr
def make_record(): return logbook.LogRecord('Test Logger', logbook.WARNING, 'Hello {foo:invalid}', kwargs={'foo': 42}, frame=sys._getframe())