[CalendarServer-changes] [11341] CalendarServer/trunk/twext/python/log.py

source_changes at macosforge.org source_changes at macosforge.org
Thu Jun 13 16:03:13 PDT 2013


Revision: 11341
          http://trac.calendarserver.org//changeset/11341
Author:   wsanchez at apple.com
Date:     2013-06-13 16:03:13 -0700 (Thu, 13 Jun 2013)
Log Message:
-----------
Try really hard to avoid exceptions in formatEvent()

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

Modified: CalendarServer/trunk/twext/python/log.py
===================================================================
--- CalendarServer/trunk/twext/python/log.py	2013-06-13 22:20:49 UTC (rev 11340)
+++ CalendarServer/trunk/twext/python/log.py	2013-06-13 23:03:13 UTC (rev 11341)
@@ -288,7 +288,10 @@
             return format.format(**event)
 
         except Exception as e:
-            return cls.formatUnformattableEvent(event, e)
+            try:
+                return cls.formatUnformattableEvent(event, e)
+            except Exception:
+                return u"MESSAGE LOST"
 
 
     @classmethod
@@ -303,12 +306,42 @@
 
         @return: a L{unicode}
         """
-        return (
-            u"Unable to format event {event}: {error}"
-            .format(event=event, error=error)
-        )
+        try:
+            return (
+                u"Unable to format event {event}: {error}"
+                .format(event=event, error=error)
+            )
+        except Exception as error:
+            #
+            # Yikes, something really nasty happened.
+            #
+            # Try to recover as much formattable data as possible;
+            # hopefully at least the namespace is sane, which will
+            # help you find the offending logger.
+            #
+            try:
+                items = []
 
+                for key, value in event.items():
+                    try:
+                        items.append(u"{key} = ".format(key=key))
+                    except Exception:
+                        items.append(u"<UNFORMATTABLE KEY> = ")
+                    try:
+                        items.append(u"{value}".format(value=value))
+                    except Exception:
+                        items.append(u"<UNFORMATTABLE VALUE>")
 
+                text = ", ".join(items)
+            except Exception:
+                text = ""
+
+            return (
+                u"MESSAGE LOST: Unformattable object logged: {error}\n"
+                u"Recoverable data: {text}"
+                .format(text=text)
+            )
+
     def emit(self, level, format=None, **kwargs):
         """
         Emit a log event to all log observers at the given level.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130613/c9998171/attachment.html>


More information about the calendarserver-changes mailing list