Ticket #5696: logging-extra-v2.patch

File logging-extra-v2.patch, 2.6 KB (added by chris-, 5 years ago)

Replaces patch logging-extra.patch

  • twisted/python/log.py

    === modified file 'twisted/python/log.py'
     
    513513    def emit(self, eventDict):
    514514        """
    515515        Receive a twisted log entry, format it and bridge it to python.
     516        The event dict is passed along to the logging module using the extra keyword.
    516517
    517518        By default the logging level used is info; log.err produces error
    518519        level, and you can customize the level by using the C{logLevel} key::
     
    529530        text = textFromEventDict(eventDict)
    530531        if text is None:
    531532            return
    532         self.logger.log(level, text)
     533        del eventDict['message']
     534        self.logger.log(level, text, extra=eventDict)
    533535
    534536    def start(self):
    535537        """
     
    703705except NameError:
    704706    defaultObserver = DefaultObserver()
    705707    defaultObserver.start()
    706 
  • twisted/test/test_log.py

    === modified file 'twisted/test/test_log.py'
     
    504504        self.assertIn("Hello!", fakeFile.getvalue())
    505505        self.assertIsInstance(sys.stdout, log.StdioOnnaStick)
    506506        self.assertEqual(sys.stdout.isError, False)
    507         self.assertEqual(sys.stdout.encoding, 
     507        self.assertEqual(sys.stdout.encoding,
    508508                         origStdout.encoding or sys.getdefaultencoding())
    509509        self.assertIsInstance(sys.stderr, log.StdioOnnaStick)
    510510        self.assertEqual(sys.stderr.isError, True)
     
    607607        self.lp.msg(message='', isError=False)
    608608        self.assertEqual(self.out.getvalue(), '')
    609609
     610    def test_extraEventDict(self):
     611        """
     612        Verify that extra eventDict data is passed to stdlib logging as extras.
     613        """
     614        self.hdlr.setFormatter(logging.Formatter(fmt="%(message)s %(foo)s"))
     615        self.lp.msg("Hello World", foo="bar")
     616
     617        self.assertIn("Hello World", self.out.getvalue())
     618        self.assertIn("bar", self.out.getvalue())
    610619
    611620class PythonLoggingIntegrationTestCase(unittest.TestCase):
    612621    """
     
    770779                         [unicodeString.encode("utf-8"),
    771780                          (u"Also, " + unicodeString).encode("utf-8"),
    772781                          unicodeString.encode("utf-8")])
    773 
  • twisted/topfiles/5696.feature

    === added file 'twisted/topfiles/5696.feature'
     
     1The PythonLoggingObserver now passes additional eventDict data to the logging module using the extra keyword.