Ticket #5696: logging-extra.patch

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

Pass eventDict along to stdlib logging as extras

  • twisted/python/log.py

    === modified file 'twisted/python/log.py'
     
    529530        text = textFromEventDict(eventDict)
    530531        if text is None:
    531532            return
    532         self.logger.log(level, text)
     533        # 'message' key is not allowed in the extra dict
     534        del eventDict['message']
     535        self.logger.log(level, text, extra=eventDict)
    533536
    534537    def start(self):
    535538        """
  • twisted/test/test_log.py

    === modified file 'twisted/test/test_log.py'
     
    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    """