Skip to content

How to supress traceback for certain packages when printing exception traces? #750

@colinrsmall

Description

@colinrsmall

We just switched to structlog in a project of ours. Previously, we were using a custom Rich handler in our logging config to supress certain packages' traces when exceptions were printed. I'm now trying to do the same by using a similar custom Rich handler in our logging config, but it seems like tracebacks are no longer being supressed. Is there a way to set this config within structlog?

Here's our rich handler:

def make_rich_handler():
    console = Console(
        width=250,
    )
    return RichHandler(
        console=console,
        tracebacks_suppress=[
            "django.core.handlers",
            "django.utils.deprecation",
            "django.db.backends",
            "asyncio",
        ],
    )

and our logging config:

{
            "version": 1,
            "disable_existing_loggers": False,
            "formatters": {
                "console_formatter": {
                    "()": structlog.stdlib.ProcessorFormatter,
                    "processor": structlog.dev.ConsoleRenderer(),
                },
            },
            "handlers": {
                "console": {
                    "()": make_rich_handler,
                    "formatter": "console_formatter",
                },
                "console_plain": {
                    "()": "logging.StreamHandler",
                    "formatter": "console_formatter",
                },
            },
            "loggers": {
                "": {  # Root logger
                    "handlers": ["console"],
                    "level": LOG_LEVEL,
                },
                "django_structlog": {
                    "handlers": ["console"],
                    "level": LOG_LEVEL,
                },
                "py.warnings": {"handlers": ["console"], "level": LOG_LEVEL, "propagate": False},
                # Temporal: force plain stdlib logging (no Rich)
                "temporalio": {"handlers": ["console_plain"], "level": LOG_LEVEL, "propagate": False},
                "temporalio.activity": {"handlers": ["console_plain"], "level": LOG_LEVEL, "propagate": False},
                "temporalio.worker": {"handlers": ["console_plain"], "level": LOG_LEVEL, "propagate": False},
                # If any Gunicorn Python loggers bubble into Django, keep them quiet
                "gunicorn": {"level": LOG_LEVEL, "propagate": False},
                "gunicorn.error": {"level": LOG_LEVEL, "propagate": False},
                "gunicorn.access": {"level": LOG_LEVEL, "propagate": False},
            },
}

structlog.configure(
    processors=[
        structlog.contextvars.merge_contextvars,
        structlog.stdlib.filter_by_level,
        structlog.processors.TimeStamper(fmt="iso"),
        structlog.stdlib.add_logger_name,
        structlog.stdlib.add_log_level,
        structlog.stdlib.PositionalArgumentsFormatter(),
        structlog.processors.UnicodeDecoder(),
        structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
    ],
    logger_factory=structlog.stdlib.LoggerFactory(),
    cache_logger_on_first_use=True,
)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions