How to use two renderers with structlog?

105 Views Asked by At

Would anyone of you know how to use two different renderers in structlog?

I'd like to use ConsoleRenderer with colors and nice formatting in console (on DEBUG) and also log same messages in log, without colors and on INFO level.

Here is my current configuration:

import logging
import logging.config
import structlog
from structlog.stdlib import BoundLogger

logging.config.dictConfig(
    {
        "version": 1,
        "disable_existing_loggers": False,
        "handlers": {
            "default": {
                "level": "DEBUG",
                "class": "logging.StreamHandler",
            },
            "file": {
                "level": "INFO",
                "class": "logging.handlers.WatchedFileHandler",
                "filename": "/var/tmp/test.log",
            },
        },
        "loggers": {
            "": {
                "handlers": ["default", "file"],
                "level": "DEBUG",
                "propagate": True,
            },
        },
    }
)


structlog.stdlib.recreate_defaults(log_level=None)
structlog.configure(
    processors=[
        structlog.contextvars.merge_contextvars,
        structlog.processors.add_log_level,
        structlog.processors.StackInfoRenderer(),
        structlog.processors.TimeStamper(fmt="iso"),
        structlog.dev.ConsoleRenderer(colors=False),
    ],
    logger_factory=structlog.stdlib.LoggerFactory(),
    wrapper_class=BoundLogger,
    cache_logger_on_first_use=True,
)
0

There are 0 best solutions below