-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added defaults parameter for logging.Formatter #85061
Comments
TLDR; Python's logging.Formatter allows the placement of custom fields, e.g. 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 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 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. |
New features will go in Python 3.10 indeed. |
It looks like |
@ayoisaiah fixing. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: