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

Merged
merged 5 commits into from
Jan 12, 2025
Merged
Changes from all commits
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
52 changes: 50 additions & 2 deletions docs/howto/production/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ messages, they are added as [extra] elements to the logs themselves.
This way, you can adapt the logs to whatever format suits your needs the most, using
a log filter:

```
```python
import logging

class ProcrastinateLogFilter(logging.Filter):
Expand All @@ -27,7 +27,55 @@ 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].

:::{note}
When using the `procrastinate` CLI, procrastinate sets up the logs for you,
but the only customization available is `--log-format` and `--log-format-style`.
If you want to customize the log format further, you will need run your own
script that calls procrastinate's app methods.
:::

## `structlog`

[`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:

```python
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.configure(
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