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.

Title: Added defaults parameter for logging.Formatter
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.10
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: bar.harel, miss-islington, remi.lapeyre, vinay.sajip
Priority: normal Keywords: patch

Created on 2020-06-05 23:29 by bar.harel, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 20668 merged bar.harel, 2020-06-05 23:38
Messages (3)
msg370796 - (view) Author: Bar Harel (bar.harel) * Date: 2020-06-05 23:29
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.
msg370802 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-06-06 00:37
New features will go in Python 3.10 indeed.
msg371816 - (view) Author: miss-islington (miss-islington) Date: 2020-06-18 14:19
New changeset 8f192d12af82c4dc40730bf59814f6a68f68f950 by Bar Harel in branch 'master':
bpo-40884: Added defaults parameter for logging.Formatter (GH-20668)
Date User Action Args
2022-04-11 14:59:32adminsetgithub: 85061
2020-06-29 17:09:05vinay.sajipsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-06-18 14:19:02miss-islingtonsetnosy: + miss-islington
messages: + msg371816
2020-06-06 00:37:53remi.lapeyresetnosy: + vinay.sajip, remi.lapeyre

messages: + msg370802
versions: - Python 3.9
2020-06-05 23:38:24bar.harelsetkeywords: + patch
stage: patch review
pull_requests: + pull_request19885
2020-06-05 23:29:51bar.harelcreate