procrastinate-org / procrastinate

PostgreSQL-based Task Queue for Python
https://procrastinate.readthedocs.io/
MIT License
834 stars 52 forks source link

How to configure with structlog? #1030

Open kasteph opened 4 months ago

kasteph commented 4 months ago

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

Originally posted by **kasteph** March 27, 2024 I am following the code snippet [here](https://github.com/procrastinate-org/procrastinate/blob/d82497ece46d20146dda461a688e200a34bf7fb1/procrastinate_demos/demo_django/project/settings.py#L131-L148) and the [docs](https://procrastinate.readthedocs.io/en/stable/howto/production/logging.html). 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`?