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

docs: add structlog config #1031

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion docs/howto/production/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,50 @@ to see them is to use a structured logging library such as [`structlog`].

If you want a minimal example of a logging setup that displays the extra
attributes without using third party logging libraries, look at the
[Django demo]
[Django demo].

## structlog
kasteph marked this conversation as resolved.
Show resolved Hide resolved

[`structlog`](https://www.structlog.org/en/stable/index.html) needs to be
configured in order to have `procrastinate`'s logs be formatted uniformly
with the rest of your application.

The `structlog` docs has a [how to](https://www.structlog.org/en/stable/standard-library.html#rendering-using-structlog-based-formatters-within-logging).

A minimal configuration would look like:

```py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I think we're using python elsewhere, rather than py)

shared_processors = [
structlog.contextvars.merge_contextvars,
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.processors.StackInfoRenderer(),
structlog.dev.set_exc_info,
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,

This appears to be the issue. If I add this processor to the end of only the processors arg to structlog.configure(), and not to the foreigh_pre_chain argument in structlog.stdlib.ProcessorFormatter(), then the rest works.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow !

I didn't commit the energy to dive into this, but it really is heartwarming to see people come and help for issues like this. Whether or not you feel like you did something awesome, I just wanted to say how pleasant it is to see this kind of contribution.

And of course, thank you a ton to the PR author too !

Let's get this merged soon :)

]

structlog.configure(
processors=shared_processors,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
processors=shared_processors,
processors=shared_processors + [structlog.stdlib.ProcessorFormatter.wrap_for_formatter],

logger_factory=structlog.stdlib.LoggerFactory(),
cache_logger_on_first_use=True,
)

formatter = structlog.stdlib.ProcessorFormatter(
foreign_pre_chain=shared_processors,
processors=[
structlog.stdlib.ProcessorFormatter.remove_processors_meta,
structlog.dev.ConsoleRenderer(event_key="message"),
],
)

handler = logging.StreamHandler()
handler.setFormatter(formatter)

root = logging.getLogger()
root.addHandler(handler)
root.setLevel(log_level)
```


[extra]: https://timber.io/blog/the-pythonic-guide-to-logging/#adding-context
[`structlog`]: https://www.structlog.org/en/stable/
Expand Down
Loading