New submission from Bar Harel <[email protected]>:
TLDR; `logging.Formatter('%(ip)s %(message)s', defaults={"ip": None})`
Python's logging.Formatter allows the placement of custom fields, e.g.
`logging.Formatter("%(ip)s %(message)")`.
If a handler has a formatter with a custom field, all log records that go
through the handler must have the custom field set using `extra={}`.
Failure to do so will result in exceptions thrown inside the logging library.
Custom fields are common, and are even suggested by the Python logging
cookbook, where they are attached to the root logger.
There is, however, no way to specify default values for the custom fields.
Quite a few issues arise from it.
For example, if I've set a formatter on the root logger with the custom field
"%(ip)s", all logging messages sent by the asyncio library, will cause
exceptions to raise.
Adding default values is possible using LoggerAdapter but will causes other
issues as well as not solve the aforementioned problem.
Adding default values is possible using Filters, but cause confusion, isn't
simple, and permanently modify the record object itself, which can cause issues
if more handlers or formatters are attached.
>From a quick search, this feature was asked for many times in stackoverflow,
>and even spawned up a few libraries such as "logaugment" in order to solve it.
I believe the solution offered, by using `defaults={}` is simple enough to not
need discussion over python-ideas, yet common enough to justify the addition to
the standard library.
I've provided a reference PR. It does not cause backwards compatibility issues,
complies with all formatter styles (%, {}, $), passes all tests and is simple
enough to both use and understand.
Not sure if 3.9 is feature-closed for small additions like this.
----------
components: Library (Lib)
messages: 370796
nosy: bar.harel
priority: normal
severity: normal
status: open
title: Added defaults parameter for logging.Formatter
type: enhancement
versions: Python 3.10, Python 3.9
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue40884>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com