From aeadedc658de60442c54966ac7788a7be06fde97 Mon Sep 17 00:00:00 2001 From: kasteph Date: Thu, 28 Mar 2024 18:45:31 +0100 Subject: [PATCH 1/3] docs: add structlog config --- docs/howto/production/logging.md | 45 +++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/docs/howto/production/logging.md b/docs/howto/production/logging.md index 086da5523..8baa70d2a 100644 --- a/docs/howto/production/logging.md +++ b/docs/howto/production/logging.md @@ -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 + +[`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 +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, +] + +structlog.configure( + processors=shared_processors, + 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/ From f00782e0bb668857ac4d0288b18707e750c6d8b9 Mon Sep 17 00:00:00 2001 From: kasteph Date: Mon, 1 Apr 2024 11:22:11 +0200 Subject: [PATCH 2/3] backticks Co-authored-by: Joachim Jablon --- docs/howto/production/logging.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/howto/production/logging.md b/docs/howto/production/logging.md index 8baa70d2a..8ad0345d2 100644 --- a/docs/howto/production/logging.md +++ b/docs/howto/production/logging.md @@ -29,7 +29,7 @@ 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]. -## structlog +## `structlog` [`structlog`](https://www.structlog.org/en/stable/index.html) needs to be configured in order to have `procrastinate`'s logs be formatted uniformly From d73cbfa29275281d59fb2ddfaf94a91fa11bcb9f Mon Sep 17 00:00:00 2001 From: Joachim Jablon Date: Sun, 7 Apr 2024 13:07:55 +0200 Subject: [PATCH 3/3] Added a note --- docs/howto/production/logging.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/docs/howto/production/logging.md b/docs/howto/production/logging.md index 8ad0345d2..5df46b07f 100644 --- a/docs/howto/production/logging.md +++ b/docs/howto/production/logging.md @@ -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): @@ -29,6 +29,13 @@ 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]. +:::{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 @@ -39,7 +46,7 @@ The `structlog` docs has a [how to](https://www.structlog.org/en/stable/standard A minimal configuration would look like: -```py +```python shared_processors = [ structlog.contextvars.merge_contextvars, structlog.stdlib.add_logger_name, @@ -71,7 +78,6 @@ 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/ [Django demo]: https://github.com/procrastinate-org/procrastinate/blob/main/procrastinate_demos/demo_django/project/settings.py#L151