def test_make_record_context_extra(): Context.new() logger = logs.StructuredLogger('test') logs.logging_context.push(a=1) record = logger.makeRecord(*record_args()) assert record.__dict__['a'] == 1 assert record._structured == {'a': 1}
def test_make_record_context_renamed(): Context.new() logger = logs.StructuredLogger('test') logs.set_global_extra({'a': 1}) logs.logging_context.push(a=2) record = logger.makeRecord(*record_args()) assert record._structured == {'a': 1, 'a_': 2}
def test_make_record_global_extra(): Context.new() logger = logs.StructuredLogger('test') logs.set_global_extra({'a': 1}) record = logger.makeRecord(*record_args()) assert record.__dict__['a'] == 1 assert record._structured == {'a': 1}
def make_record(extra, msg='msg here'): """Make a test record from StructuredLogger.""" logger = logs.StructuredLogger('test') record = logger.makeRecord(*record_args(msg), extra=extra) # stub out the time record.__dict__['created'] = TIME record.msecs = MSEC return record
def test_make_record_context_overiddes(): logger = logs.StructuredLogger('test') logger.update_extra({'a': 1}) logs.set_logging_context(a=2) record = logger.makeRecord(*record_args()) assert record.__dict__['a'] == 2 assert record._structured == {'a': 2}
def test_make_record_all_extra(): logger = logs.StructuredLogger('test') logger.update_extra({'a': 1}) logs.set_logging_context(b=2) record = logger.makeRecord(*record_args(), extra={'c': 3}) assert record.__dict__['a'] == 1 assert record.__dict__['b'] == 2 assert record.__dict__['c'] == 3 assert record._structured == {'a': 1, 'b': 2, 'c': 3}
def test_make_record_protected(monkeypatch): def error(*args): raise Exception() monkeypatch.setattr(logs.logging_context, 'get', error) logger = logs.StructuredLogger('test') record = logger.makeRecord(*record_args('test'), extra={'foo': 'bar'}) assert record.foo == 'bar' assert record.extra == {'foo': 'bar'} assert record._trailer is None
def test_make_record_ordering(): logger = logs.StructuredLogger('test') logs.set_global_extra({'global': 1}) logs.logging_context.push(context=2) extra = OrderedDict() extra['user1'] = 3 extra['user2'] = 4 record = logger.makeRecord(*record_args(), extra=extra) assert list( record._structured.keys()) == ['user1', 'user2', 'context', 'global']
def test_logger_collects_raven_breadcrumbs(): logger = logs.StructuredLogger('test') with raven.context.Context() as ctx: logger.info('info', extra={'foo': 'bar'}) logger.debug('debug', extra={'foo': 'bar'}) breadcrumbs = ctx.breadcrumbs.get_buffer() assert len(breadcrumbs) == 1 assert breadcrumbs[0]['message'] == 'info' assert breadcrumbs[0]['level'] == 'info' assert breadcrumbs[0]['category'] == 'test' assert breadcrumbs[0]['data'] == {'extra': {'foo': 'bar'}}
def test_make_record_no_extra(): logger = logs.StructuredLogger('test') record = logger.makeRecord(*record_args()) assert record._structured == {}
def test_make_record_extra_renamed(): logger = logs.StructuredLogger('test') logs.set_global_extra({'a': 1}) record = logger.makeRecord(*record_args(), extra={'a': 2}) assert record._structured == {'a': 1, 'a_': 2}
def test_make_record_context_renamed(): logger = logs.StructuredLogger('test') logger.update_extra({'a': 1}) logs.set_logging_context(a=2) record = logger.makeRecord(*record_args()) assert record._structured == {'a': 1, 'a_': 2}