wimglenn / pytest-structlog

Structured logging assertions
MIT License
54 stars 6 forks source link

Display Traceback in captured logs for `logger.exception` #32

Closed KyleKing closed 3 months ago

KyleKing commented 3 months ago

Could we conditionally specify that additional processors should be kept? Or at least allow the format_exc processor to be configurable?

Without the format_exc processor, the logs have exc_info=True, but it would be useful for debugging and testing if the full traceback could be captured

Would it be possible to have the exception populated using the logic from structlog ()?

Based on an existing test, I think this would be a minimal test case:

import logging.config

import pytest
import structlog

from pytest_structlog import StructuredLogCapture

@pytest.fixture
def stdlib_bound_logger_configure():
    structlog.configure(
        processors=[
            structlog.processors.format_exc_info,
            structlog.processors.JSONRenderer(),
        ],
        logger_factory=structlog.stdlib.LoggerFactory(),
        wrapper_class=structlog.stdlib.BoundLogger,
    )

def log_exception():
    logger = structlog.get_logger()
    try:
        1 / 0
    except ZeroDivisionError:
        logger.exception("event_name")

def test_exception_level(stdlib_bound_logger_configure, log: StructuredLogCapture):
    log_exception()
    assert isinstance(log.events[0]["exc_info"], str)
wimglenn commented 3 months ago

Hello @KyleKing, I've a proposed fix in https://github.com/wimglenn/pytest-structlog/pull/33 Can you check if it is satisfactory before I tag/release? You can install that from test PyPI:

pip install --index-url=https://test.pypi.org/simple 'pytest-structlog==0.9'

I'm procrastinating configuration of the plugin until pytest upstream can detangle the config object.