Closed
Labels
3.8 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement
Description
| BPO | 34844 |
|---|---|
| Nosy | @gvanrossum, @vsajip, @BNMetrics |
| PRs |
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:
assignee = None
closed_at = <Date 2018-10-16.06:44:50.300>
created_at = <Date 2018-09-29.20:17:10.806>
labels = ['3.8', 'type-feature', 'library']
title = 'logging.Formatter enhancement - Checking on style and fmt fields'
updated_at = <Date 2018-10-16.06:44:50.299>
user = 'https://github.com/BNMetrics'bugs.python.org fields:
activity = <Date 2018-10-16.06:44:50.299>
actor = 'vinay.sajip'
assignee = 'none'
closed = True
closed_date = <Date 2018-10-16.06:44:50.300>
closer = 'vinay.sajip'
components = ['Library (Lib)']
creation = <Date 2018-09-29.20:17:10.806>
creator = 'BNMetrics'
dependencies = []
files = []
hgrepos = []
issue_num = 34844
keywords = ['patch']
message_count = 7.0
messages = ['326690', '326691', '326763', '326829', '327108', '327772', '327775']
nosy_count = 3.0
nosy_names = ['gvanrossum', 'vinay.sajip', 'BNMetrics']
pr_nums = ['9703']
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue34844'
versions = ['Python 3.8']Metadata
Metadata
Assignees
Labels
3.8 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement
Activity
BNMetrics commentedon Sep 29, 2018
Issue: Currently logging.Formatter does not check if the format passed in is valid style or if the field is valid when creating the logging.Formatter object. It would be nice to have such check in the constructor of the logging.Formatter.
Here are 2 scenarios:
Scenario 1: Passing in invalid
fmtwhen creating the logging.Formatter.Example:
import logging
logger = logging.getLogger('my_logger')
handler = logging.StreamHandler()
The above example would output the fmt string("blah-blah") whenever a logging operation is performed, such as
logging.info,logging.warning.And this goes the same for mismatching style and fmt, like so:
fmt = logging.Formatter("{asctime}-{message}", style="%")
Scenario 2: Passing in invalid fields to logging.Formatter
--- Logging error ---
Personally, I think the "KeyError" here can be misleading and confusing to some.
Proposal:
I would like to make a PR with the following changes:
Checking fmt to match the style in the constructor of logging.Formatting
When calling log message functions such as "logger.info()", an "extra" key word arg can be passed for custom format fields,
for example:
fmt = logging.Formatter("{cpuUsage} - {message}", style="{") # In this case, cpuUsage would be custom
logger.info("my logging message", extra={"cpuUsage": "my_cpu_usage"})
Draw Backs:
Please let me know your thoughts, and I thought it would be nice to get some new ideas and areas I haven't thought about before I make this PR.
Best regards,
Luna Chen (BNMetrics)
[-]logging.Formatter enhancement - Checking on style and[/-][+]logging.Formatter enhancement - Checking on style and fmt fields[/+]gvanrossum commentedon Sep 29, 2018
Moving to Python 3.8 because this is a feature proposal. Adding Vinay to nosy list because this is about logging.
vsajip commentedon Oct 1, 2018
This seems a reasonable change to want to make. You would need to parse the format string for fields using the appropriate style. This should probably be via a validate() method in each of the XXXStyle classes, which is passed the format string and raises an exception if invalid.
If this is just a list of custom field names, it could be inferred from the passed format string, which will now be being parsed for fields for the checking described above. So there should be no need to pass an additional argument.
We can't do this, because of the need to maintain backwards compatibility. Note also that custom fields can be introduced into a LogRecord in other ways, e.g. using Filters.
It seems reasonable to make a change to re-raise such a KeyError using a more informative error message, perhaps as a ValueError.
BNMetrics commentedon Oct 1, 2018
Thank you Vinay!
I think you have some good points! :)
I'm going to make the following changes
Best regards,
Luna Chen
vsajip commentedon Oct 5, 2018
I see a PR has been added, I'll start to review it after the CLA has been signed.
vsajip commentedon Oct 15, 2018
New changeset 18fb1fb by Vinay Sajip (BNMetrics) in branch 'master':
bpo-34844: logging.Formatter enhancement - Ensure style and format string matches in logging.Formatter (GH-9703)
18fb1fb
gvanrossum commentedon Oct 15, 2018
W00t! Congrats Luna and thanks for your contribution. Thanks Vinay for the prompt reviews!