-
Notifications
You must be signed in to change notification settings - Fork 202
master: Fix LogForwarder in case an own LogRecordFactory is used #1341
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
Conversation
b80833a to
bf2a417
Compare
|
|
||
| levelno = int(level_s) | ||
| # See logging.Handler.makeRecord() | ||
| record = logging.LogRecord( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the only use of this in mitogen.*
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. A tweak to the new test is needed, after which I'll be happy to merge
…tory is modified
The test currently fails with the following error:
$ PYTHONPATH=$(pwd)/tests:$PYTHONPATH python3 -m unittest -v log_handler_test
...
test_logrecordfactory (log_handler_test.LogRecordFactoryTest.test_logrecordfactory) ... --- Logging error ---
Traceback (most recent call last):
File "/usr/lib/python3.12/logging/__init__.py", line 464, in format
return self._format(record)
^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/logging/__init__.py", line 460, in _format
return self._fmt % values
~~~~~~~~~~^~~~~~~~
KeyError: 'custom_attribute'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.12/logging/__init__.py", line 1160, in emit
msg = self.format(record)
^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/logging/__init__.py", line 999, in format
return fmt.format(record)
^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/logging/__init__.py", line 999, in format
return fmt.format(record)
^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/logging/__init__.py", line 706, in format
s = self.formatMessage(record)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/logging/__init__.py", line 675, in formatMessage
return self._style.format(record)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/logging/__init__.py", line 466, in format
raise ValueError('Formatting field not found in record: %s' % e)
ValueError: Formatting field not found in record: 'custom_attribute'
Signed-off-by: Marc Hartmayer <[email protected]>
Since Python 3.2 the log record factory can be changed by using `logging.setLogRecordFactory` [1]. Therefore use `logging.makeLogRecord` as recommended in the documentation: "LogRecord instances are created automatically by the Logger every time something is logged, and can be created manually via makeLogRecord() (for example, from a pickled event received over the wire)." [2] This fixes the test case `log_handler_test.LogRecordFactoryTest.test_logrecordfactory`. [1] https://docs.python.org/3/library/logging.html#logging.setLogRecordFactory [2] https://docs.python.org/3/library/logging.html#logrecord-objects Signed-off-by: Marc Hartmayer <[email protected]>
bf2a417 to
2474518
Compare
|
Thanks for merging! I missed to add a changelog entry.... shall I add it or will you do? |
|
Thanks for the fix. I did the changelog in #1346 |
Since Python 3.2 the log record factory can be changed by using
logging.setLogRecordFactory[1]. Therefore uselogging.makeLogRecordas recommended in the documentation:"LogRecord instances are created automatically by the Logger every time something is logged, and can be created manually via makeLogRecord() (for example, from a pickled event received over the wire)." [2]
This fixes the following error for my use case:
--- Logging error ---
Traceback (most recent call last):
File "/usr/lib/python3.12/logging/init.py", line 464, in format
return self._format(record)
^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/logging/init.py", line 460, in _format
return self._fmt % values
~~~~~~~~~~^~~~~~~~
KeyError: 'relpathname'
[1] https://docs.python.org/3/library/logging.html#logging.setLogRecordFactory
[2] https://docs.python.org/3/library/logging.html#logrecord-objects