Bugfix using the getMessage method to get a LogRecord msg as a string #10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull request to fix a bug in the Ratelimitingfilter.
I noticed a bug when I attempted to call
logger.exception(ValueError("This is a error"))
. This is because in ratelimitingfilter.py, the log message is extracted from the LogRecord using LogRecord.msg. However, ratelimitingfilter assumes that this msg is always a string. This is not actually the case, particularly when it comes to exceptions.Documentation on this can be found here: https://docs.python.org/3.9/howto/logging.html#arbitrary-object-messages
For example, change the last test in the code to the following:
And run the tests, you'll see the bug. Ratelimitingfilterfails at any point when it assumes that the msg is a string and attempts to iterate through it, as the LogRecord.msg is actually the RuntimeError exception object and is not iterable.
To fix this bug, I have changed references to LogRecord.msg to LogRecord.getMessage(), which runs str on the message before passing it to you. Details on the method are here: https://docs.python.org/3.9/library/logging.html#logging.LogRecord.getMessage
I have also doubled checked, and even with the changes I still get the full stack trace when logging an exception.