Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to configure with structlog? #1030

Open
kasteph opened this issue Mar 28, 2024 Discussed in #1028 · 0 comments · May be fixed by #1031
Open

How to configure with structlog? #1030

kasteph opened this issue Mar 28, 2024 Discussed in #1028 · 0 comments · May be fixed by #1031

Comments

@kasteph
Copy link

kasteph commented Mar 28, 2024

Discussed in #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 kasteph linked a pull request Mar 28, 2024 that will close this issue
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant