Skip to content

Commit

Permalink
add some note about logging configuration (#609)
Browse files Browse the repository at this point in the history
* add some note about logging configuration

* use {note}

* Minor adjustments

---------

Co-authored-by: Hynek Schlawack <hs@ox.cx>
  • Loading branch information
methane and hynek committed Apr 2, 2024
1 parent c34da02 commit f26afb0
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/standard-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ The most straight-forward option is to configure standard library `logging` clos
Since these are usually log entries from third parties that don't take advantage of *structlog*'s features, this is surprisingly often a perfectly adequate approach.

For instance, if you log JSON in production, configure `logging` to use [*python-json-logger*] to make it print JSON too, and then tweak the configuration to match their outputs.
You can also use {class}`~structlog.stdlib.ProcessorFormatter` as a formatter for `logging` to get the same output for both *structlog* and `logging` log entries -- see [below](processor-formatter) for an example.

:::{note}
If you want to use same file (for example, `sys.stdout` or `sys.stderr`) for both *structlog* and `logging.StreamHandler` output, you must use {class}`~structlog.WriteLogger` instead of {class}`~structlog.PrintLogger`.

This is because {class}`~structlog.PrintLogger` uses `print(log, file=file, flush=True)` to write log, and `print` writes the `log` message and a newline ("\n") to the stream separately.
This can cause interleaving of log entries from *structlog* and `logging` loggers.
{class}`~structlog.WriteLogger` writes log entries atomically to the file (for example, `file.write(log+"\n")`).
:::


### Rendering Within *structlog*
Expand Down

0 comments on commit f26afb0

Please sign in to comment.