This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author bar.harel
Recipients bar.harel
Date 2020-06-05.23:29:51
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1591399791.89.0.0887642700869.issue40884@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2020-06-05 23:29:51bar.harelsetrecipients: + bar.harel
2020-06-05 23:29:51bar.harelsetmessageid: <1591399791.89.0.0887642700869.issue40884@roundup.psfhosted.org>
2020-06-05 23:29:51bar.harellinkissue40884 messages
2020-06-05 23:29:51bar.harelcreate