def test_adapter_extra(self): """Check that extra attrs override defaults.""" with mock_tracer("trace_id", "span_id", True): logger = logging.getLogger('a.b.c') adapted_logger = log.TraceLoggingAdapter(logger, { 'otherKey': "other val", 'traceId': "override one" }) with self.assertLogs('a.b.c', level=logging.INFO) as cm1: adapted_logger.info("test message") with self.assertLogs('a.b.c', level=logging.INFO) as cm2: adapted_logger.info("test message", extra={ 'anotherKey': "other val", 'traceId': "override two" }) self.assertIsInstance(logger, logging.Logger) self.assertEqual(len(cm1.records), 1) [r1] = cm1.records self.assertEqual(r1.otherKey, "other val") self.assertEqual(r1.traceId, "override one") self.assertEqual(r1.spanId, "span_id") self.assertEqual(r1.traceSampled, True) self.assertEqual(r1.message, "test message") self.assertIsInstance(logger, logging.Logger) self.assertEqual(len(cm2.records), 1) [r2] = cm2.records self.assertEqual(r2.traceId, "override two") self.assertEqual(r2.spanId, "span_id") self.assertEqual(r2.traceSampled, True) self.assertEqual(r2.message, "test message")
def test_adapter_lr_attrs(self): """Check that logging adapter adds OC attrs.""" with mock_tracer("trace_id", "span_id", True): logger = logging.getLogger('a.b.c') adapted_logger = log.TraceLoggingAdapter(logger, {}) with self.assertLogs('a.b.c', level=logging.INFO) as cm: adapted_logger.info("test message") self.assertIsInstance(logger, logging.Logger) self.assertEqual(len(cm.records), 1) [record] = cm.records self.assertEqual(record.traceId, "trace_id") self.assertEqual(record.spanId, "span_id") self.assertEqual(record.traceSampled, True) self.assertEqual(record.message, "test message")