r47347 - Actually log exceptions to stdlib logging.

mithrandi-TA+aISz0psMTMxyoc4vAAJOcrHinNvQL0E9HWUfgJXw@public.gmane.org Thu, 5 May 2016 21:04:53 -0600 (MDT)
Newsgroups gmane.comp.python.twisted.commits
Message-ID <[email protected]>
Author: mithrandi
Date: Thu May  5 21:04:49 2016
New Revision: 47347

Modified:
   branches/stdlib-exception-logging-8316/twisted/logger/_stdlib.py
   branches/stdlib-exception-logging-8316/twisted/logger/test/test_stdlib.py

Log:
Actually log exceptions to stdlib logging.

Modified: branches/stdlib-exception-logging-8316/twisted/logger/_stdlib.py
==============================================================================
--- branches/stdlib-exception-logging-8316/twisted/logger/_stdlib.py	(original)
+++ branches/stdlib-exception-logging-8316/twisted/logger/_stdlib.py	Thu May  5 21:04:49 2016
@@ -105,8 +105,15 @@
         Format an event and bridge it to Python logging.
         """
         level = event.get("log_level", LogLevel.info)
+        failure = event.get('log_failure')
+        if failure is None:
+            exc_info = None
+        else:
+            exc_info = (
+                failure.type, failure.value, failure.getTracebackObject())
         stdlibLevel = toStdlibLogLevelMapping.get(level, stdlibLogging.INFO)
-        self.logger.log(stdlibLevel, StringifiableFromEvent(event))
+        self.logger.log(
+            stdlibLevel, StringifiableFromEvent(event), exc_info=exc_info)
 
 
 

Modified: branches/stdlib-exception-logging-8316/twisted/logger/test/test_stdlib.py
==============================================================================
--- branches/stdlib-exception-logging-8316/twisted/logger/test/test_stdlib.py	(original)
+++ branches/stdlib-exception-logging-8316/twisted/logger/test/test_stdlib.py	Thu May  5 21:04:49 2016
@@ -12,9 +12,10 @@
 
 from zope.interface.verify import verifyObject, BrokenMethodImplementation
 
+from twisted.python.compat import _PY3, currentframe
+from twisted.python.failure import Failure
 from twisted.trial import unittest
 
-from twisted.python.compat import _PY3, currentframe
 from .._levels import LogLevel
 from .._observer import ILogObserver
 from .._stdlib import STDLibLogObserver
@@ -183,6 +184,35 @@
         self.assertEqual(str(records[0].msg), "")
 
 
+    def test_failure(self):
+        """
+        An event with a failure logs the failure details as well.
+        """
+        try:
+            1 / 0
+        except ZeroDivisionError:
+            failure = Failure()
+        event = dict(log_format='Hi mom', who='me', log_failure=failure)
+        records, output = self.logEvent(event)
+        self.assertEqual(len(records), 1)
+        self.assertIn(u'ZeroDivisionError', output)
+
+
+    def test_cleanedFailure(self):
+        """
+        An event with a cleaned failure logs the failure details as well.
+        """
+        try:
+            1 / 0
+        except ZeroDivisionError:
+            failure = Failure()
+        failure.cleanFailure()
+        event = dict(log_format='Hi mom', who='me', log_failure=failure)
+        records, output = self.logEvent(event)
+        self.assertEqual(len(records), 1)
+        self.assertIn(u'ZeroDivisionError', output)
+
+
 
 class StdlibLoggingContainer(object):
     """