[CalendarServer-changes] [11676] CalendarServer/trunk/twext/python

source_changes at macosforge.org source_changes at macosforge.org
Thu Sep 12 13:47:44 PDT 2013


Revision: 11676
          http://trac.calendarserver.org//changeset/11676
Author:   wsanchez at apple.com
Date:     2013-09-12 13:47:44 -0700 (Thu, 12 Sep 2013)
Log Message:
-----------
More unformattable event tests.

Modified Paths:
--------------
    CalendarServer/trunk/twext/python/log.py
    CalendarServer/trunk/twext/python/test/test_log.py

Modified: CalendarServer/trunk/twext/python/log.py
===================================================================
--- CalendarServer/trunk/twext/python/log.py	2013-09-12 18:06:54 UTC (rev 11675)
+++ CalendarServer/trunk/twext/python/log.py	2013-09-12 20:47:44 UTC (rev 11676)
@@ -152,8 +152,7 @@
 
 
 LogLevel._levelPriorities = dict(
-    (constant, idx)
-    for (idx, constant) in
+    (constant, idx) for (idx, constant) in
     (enumerate(LogLevel.iterconstants()))
 )
 
@@ -210,11 +209,7 @@
         return formatWithCall(format, event)
 
     except BaseException as e:
-        try:
-            return formatUnformattableEvent(event, e)
-        except:
-            # This should never happen...
-            return u"MESSAGE LOST COMPLETELY"
+        return formatUnformattableEvent(event, e)
 
 
 
@@ -242,8 +237,9 @@
         # hopefully at least the namespace is sane, which will
         # help you find the offending logger.
         #
-        failure = Failure()
         try:
+            failure = Failure()
+
             items = []
 
             for key, value in event.items():
@@ -260,18 +256,19 @@
                 items.append(" = ".join((keyFormatted, valueFormatted)))
 
             text = ", ".join(items)
+
+            return (
+                u"MESSAGE LOST: unformattable object logged: {error}\n"
+                u"Recoverable data: {text}\n"
+                u"Exception during formatting:\n{failure}"
+                .format(error=error, failure=failure, text=text)
+            )
         except BaseException as e:
-            text = "UNABLE TO RECOVER ANY DATA FROM MESSAGE: {e}".format(e=e)
+            # This should never happen...
+            return u"MESSAGE LOST: unable to recover any data from message: {e}".format(e=e)
 
-        return (
-            u"MESSAGE LOST: unformattable object logged.\n"
-            u"Recoverable data: {text}\n"
-            u"Exception during formatting:\n{failure}"
-            .format(error=error, failure=failure, text=text)
-        )
 
 
-
 class Logger(object):
     """
     Logging object.
@@ -420,10 +417,10 @@
     """
 
     def __init__(self, logger=None):
-        if logger is not None:
-            self.newStyleLogger = logger
-        else:
+        if logger is None:
             self.newStyleLogger = Logger(Logger._namespaceFromCallingContext())
+        else:
+            self.newStyleLogger = logger
 
 
     def __getattribute__(self, name):

Modified: CalendarServer/trunk/twext/python/test/test_log.py
===================================================================
--- CalendarServer/trunk/twext/python/test/test_log.py	2013-09-12 18:06:54 UTC (rev 11675)
+++ CalendarServer/trunk/twext/python/test/test_log.py	2013-09-12 20:47:44 UTC (rev 11676)
@@ -23,7 +23,7 @@
 from twext.python.log import (
     LogLevel, InvalidLogLevelError,
     pythonLogLevelMapping,
-    formatEvent, formatWithCall,
+    formatEvent, formatUnformattableEvent, formatWithCall,
     Logger, LegacyLogger,
     ILogObserver, LogPublisher,
     FilteringLogObserver, PredicateResult,
@@ -67,8 +67,8 @@
 
 
 class TestLegacyLogger(LegacyLogger):
-    def __init__(self):
-        LegacyLogger.__init__(self, logger=TestLogger())
+    def __init__(self, logger=TestLogger()):
+        LegacyLogger.__init__(self, logger=logger)
 
 
 
@@ -280,11 +280,6 @@
          """
          Formatting an unformattable event that has an unformattable key.
          """
-         class Gurk(object):
-             # Class that raises in C{__repr__()}.
-             def __repr__(self):
-                 return str(1/0)
-
          event = {
              "log_format": "{evil()}",
              "evil": lambda: 1/0,
@@ -292,21 +287,15 @@
          }
          result = formatEvent(event)
 
-         self.assertIn("MESSAGE LOST: unformattable object logged.", result)
+         self.assertIn("MESSAGE LOST: unformattable object logged:", result)
          self.assertIn("Recoverable data:", result)
          self.assertIn("Exception during formatting:", result)
-         #self.assertIn(repr(event), result)
 
 
     def test_formatUnformattableEventWithUnformattableValue(self):
          """
          Formatting an unformattable event that has an unformattable value.
          """
-         class Gurk(object):
-             # Class that raises in C{__repr__()}.
-             def __repr__(self):
-                 return str(1/0)
-
          event = dict(
              log_format="{evil()}",
              evil=lambda: 1/0,
@@ -314,13 +303,27 @@
          )
          result = formatEvent(event)
 
-         self.assertIn("MESSAGE LOST: unformattable object logged.", result)
+         self.assertIn("MESSAGE LOST: unformattable object logged:", result)
          self.assertIn("Recoverable data:", result)
          self.assertIn("Exception during formatting:", result)
-         #self.assertIn(repr(event), result)
 
 
+    def test_formatUnformattableEventWithUnformattableErrorOMGWillItStop(self):
+         """
+         Formatting an unformattable event that has an unformattable value.
+         """
+         event = dict(
+             log_format="{evil()}",
+             evil=lambda: 1/0,
+         )
 
+         # Call formatUnformattableEvent() directly with a bogus exception.
+         result = formatUnformattableEvent(event, Gurk())
+
+         self.assertIn("MESSAGE LOST: unable to recover any data from message:", result)
+
+
+
 class LoggerTests(SetUpTearDown, unittest.TestCase):
     """
     Tests for L{Logger}.
@@ -628,6 +631,14 @@
     Tests for L{LegacyLogger}.
     """
 
+    def test_namespace_default(self):
+        """
+        Default namespace is module name.
+        """
+        log = TestLegacyLogger(logger=None)
+        self.assertEquals(log.newStyleLogger.namespace, __name__)
+
+
     def test_passThroughAttributes(self):
         """
         C{__getattribute__} on L{LegacyLogger} is passing through to Twisted's
@@ -763,3 +774,10 @@
 
         for key, value in kwargs.items():
             self.assertIdentical(log.newStyleLogger.emitted["kwargs"][key], value)
+
+
+
+class Gurk(object):
+    # Class that raises in C{__repr__()}.
+    def __repr__(self):
+        return str(1/0)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130912/b63ec3d5/attachment.html>


More information about the calendarserver-changes mailing list