procrastinate icon indicating copy to clipboard operation
procrastinate copied to clipboard

How to configure with structlog?

Open kasteph opened this issue 3 months ago • 0 comments

Discussed in https://github.com/procrastinate-org/procrastinate/discussions/1028

Originally posted by kasteph March 27, 2024 I am following the code snippet here and the docs. I have something like this:

import logging
import sys

import structlog


class ProcrastinateLogFilter(logging.Filter):
    _reserved_log_keys = frozenset(
        """args asctime created exc_info exc_text filename
        funcName levelname levelno lineno module msecs message msg name pathname
        process processName relativeCreated stack_info thread threadName""".split()
    )

    def filter(self, record: logging.LogRecord):
        record.procrastinate = {}
        for key, value in vars(record).items():
            if not key.startswith("_") and key not in self._reserved_log_keys | {
                "procrastinate"
            }:
                record.procrastinate[key] = value  # type: ignore
        return True


def configure_logging(env_settings) -> bool:
    logging.getLogger("uvicorn.error").disabled = True
    logging.getLogger("uvicorn.access").disabled = True
    logging.getLogger("procrastinate").addFilter(ProcrastinateLogFilter)

    logging.basicConfig(format="%(message)s", stream=sys.stdout, level=env_settings.log_level)
    renderer = structlog.processors.JSONRenderer() if env_settings.prod else structlog.dev.ConsoleRenderer()
    structlog.configure(
        processors=[
            structlog.stdlib.add_log_level,
            structlog.contextvars.merge_contextvars,
            structlog.processors.StackInfoRenderer(),
            structlog.dev.set_exc_info,
            structlog.processors.TimeStamper(fmt="iso", utc=True),
            renderer,
        ],
        logger_factory=structlog.PrintLoggerFactory(),

However, this doesn't actually work. Can someone share a working example of configuring with structlog?

kasteph avatar Mar 28 '24 17:03 kasteph