MCPcopy Create free account
hub / github.com/hynek/structlog / test_logrecord_exc_info

Method test_logrecord_exc_info

tests/test_stdlib.py:1445–1495  ·  view source on GitHub ↗

LogRecord.exc_info is set consistently for structlog and non-structlog log records.

(self)

Source from the content-addressed store, hash-verified

1443 logger.info("baz")
1444
1445 def test_logrecord_exc_info(self):
1446 """
1447 LogRecord.exc_info is set consistently for structlog and non-structlog
1448 log records.
1449 """
1450 configure_logging(None)
1451
1452 # This doesn't test ProcessorFormatter itself directly, but it's
1453 # relevant to setups where ProcessorFormatter is used, i.e. where
1454 # handlers will receive LogRecord objects that come from both structlog
1455 # and non-structlog loggers.
1456
1457 records: dict[str, logging.LogRecord] = {}
1458
1459 class DummyHandler(logging.Handler):
1460 def emit(self, record):
1461 # Don't do anything; just store the record in the records dict
1462 # by its message, so we can assert things about it.
1463 if isinstance(record.msg, dict):
1464 records[record.msg["event"]] = record
1465 else:
1466 records[record.msg] = record
1467
1468 stdlib_logger = logging.getLogger()
1469 structlog_logger = get_logger()
1470
1471 # It doesn't matter which logger we add the handler to here.
1472 stdlib_logger.addHandler(DummyHandler())
1473
1474 try:
1475 raise Exception("foo")
1476 except Exception:
1477 stdlib_logger.exception("bar")
1478 structlog_logger.exception("baz")
1479
1480 stdlib_record = records.pop("bar")
1481
1482 assert "bar" == stdlib_record.msg
1483 assert stdlib_record.exc_info
1484 assert Exception is stdlib_record.exc_info[0]
1485 assert ("foo",) == stdlib_record.exc_info[1].args
1486
1487 structlog_record = records.pop("baz")
1488
1489 assert "baz" == structlog_record.msg["event"]
1490 assert True is structlog_record.msg["exc_info"]
1491 assert structlog_record.exc_info
1492 assert Exception is structlog_record.exc_info[0]
1493 assert ("foo",) == structlog_record.exc_info[1].args
1494
1495 assert not records
1496
1497 def test_use_get_message_false(self):
1498 """

Callers

nothing calls this directly

Calls 5

get_loggerFunction · 0.90
configure_loggingFunction · 0.85
DummyHandlerClass · 0.85
addHandlerMethod · 0.80
exceptionMethod · 0.45

Tested by

no test coverage detected