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

multiple processors to structlog.stdlib.ProcessorFormatter #334

Closed
Akuli opened this issue Jul 14, 2021 · 3 comments
Closed

multiple processors to structlog.stdlib.ProcessorFormatter #334

Akuli opened this issue Jul 14, 2021 · 3 comments

Comments

@Akuli
Copy link

Akuli commented Jul 14, 2021

I want structlog messages and stdlib logging to go to stdout and json file. I want timestamps in json file, but not in stdout.

Currently I have to do this (based on this part of the docs):

add_timestamp = structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M:%S")
render_json = structlog.processors.JSONRenderer()

def add_timestamp_and_render_json(logger, name, event_dict):
    event_dict = add_timestamp(logger, name, event_dict)
    event_dict = render_json(logger, name, event_dict)
    return event_dict

logging.config.dictConfig({
    ...
    "formatters": {
        "json_formatter": {
            "()": structlog.stdlib.ProcessorFormatter,
            "foreign_pre_chain": [
                structlog.stdlib.add_logger_name,
                structlog.stdlib.add_log_level,
            ],
            "processor": add_timestamp_and_render_json,
        },
        "stdout_formatter": {
            "()": structlog.stdlib.ProcessorFormatter,
            "foreign_pre_chain": [
                structlog.stdlib.add_logger_name,
                structlog.stdlib.add_log_level,
            ],
            "processor": structlog.dev.ConsoleRenderer(),
        },
    },
    ...
})

Would be nice if I could provide a chain instead of just one processor:

        "json_formatter": {
            ...
            "processor_chain": [
                structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M:%S"),
                structlog.processors.JSONRenderer(),
            ]
        },
@hynek
Copy link
Owner

hynek commented Jul 14, 2021

Have you tried https://pypi.org/project/multistructlog/ ?

@Akuli
Copy link
Author

Akuli commented Jul 14, 2021

Yes, I have considered using multistructlog. I looked at its source code, and it's very similar to my current structlog config. It doesn't seem to have any way to give a timestamp for one ProcessorFormatter but not the other.

@hynek
Copy link
Owner

hynek commented Nov 19, 2021

fixed by #365

@hynek hynek closed this as completed Nov 19, 2021
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.

2 participants