Skip to content

Conversation

@mhartmay
Copy link
Contributor

@mhartmay mhartmay commented Oct 23, 2025

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 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

@mhartmay mhartmay force-pushed the logforwarder-fix branch 2 times, most recently from b80833a to bf2a417 Compare October 23, 2025 13:44

levelno = int(level_s)
# See logging.Handler.makeRecord()
record = logging.LogRecord(
Copy link
Member

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.*

Copy link
Member

@moreati moreati left a 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]>
@moreati moreati dismissed their stale review October 30, 2025 08:48

resolved

@moreati moreati merged commit 4824372 into mitogen-hq:master Oct 30, 2025
26 checks passed
@mhartmay
Copy link
Contributor Author

Thanks for merging!

I missed to add a changelog entry.... shall I add it or will you do?

@mhartmay mhartmay deleted the logforwarder-fix branch October 30, 2025 08:51
@moreati
Copy link
Member

moreati commented Oct 30, 2025

Thanks for the fix. I did the changelog in #1346

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants