-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
logging.Formatter enhancement - Checking on style and fmt fields #79025
Comments
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 fmt = logging.Formatter("blah-blah", style="{")
handler.setFormatter(fmt)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
logger.info('This is my logging message.') The above example would output the fmt string("blah-blah") whenever a logging operation is performed, such as Scenario 2: Passing in invalid fields to logging.Formatter import logging
logger = logging.getLogger('my_logger')
handler = logging.StreamHandler()
fmt = logging.Formatter("%(FuncName)s-%(message)s")
handler.setFormatter(fmt)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
logger.info('This is my logging message.'
)
As you can see from the above example, the "%(FuncName)s" field is misspelled with a capital "F", which should have been a lowercase "f". In this scenario, we would get an interesting stacktrace: --- Logging error --- Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 992, in emit
msg = self.format(record)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 838, in format
return fmt.format(record)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 578, in format
s = self.formatMessage(record)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 547, in formatMessage
return self._style.format(record)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 391, in format
return self._fmt % record.__dict__
KeyError: 'FuncName'
Call stack:
File "<stdin>", line 1, in <module>
Message: 'This is my logging message.'
Arguments: () 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:
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, |
Moving to Python 3.8 because this is a feature proposal. Adding Vinay to nosy list because this is about logging. |
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. |
Thank you Vinay! I'm going to make the following changes
Best regards, |
I see a PR has been added, I'll start to review it after the CLA has been signed. |
W00t! Congrats Luna and thanks for your contribution. Thanks Vinay for the prompt reviews! |
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: