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

source_changes at macosforge.org source_changes at macosforge.org
Thu Jun 13 17:51:23 PDT 2013


Revision: 11363
          http://trac.calendarserver.org//changeset/11363
Author:   wsanchez at apple.com
Date:     2013-06-13 17:51:23 -0700 (Thu, 13 Jun 2013)
Log Message:
-----------
Make formatEvent() a function instead of a method on Logger.

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

Modified: CalendarServer/trunk/twext/python/log.py
===================================================================
--- CalendarServer/trunk/twext/python/log.py	2013-06-14 00:38:43 UTC (rev 11362)
+++ CalendarServer/trunk/twext/python/log.py	2013-06-14 00:51:23 UTC (rev 11363)
@@ -64,6 +64,7 @@
     "logLevelForNamespace",
     "setLogLevelForNamespace",
     "clearLogLevels",
+    "formatEvent",
     "Logger",
     "LegacyLogger",
     "ILogObserver",
@@ -204,6 +205,90 @@
 # Loggers
 ##
 
+def formatEvent(event):
+    """
+    Formats an event as a L{unicode}, using the format in
+    C{event["log_format"]}.  This implementation should never
+    raise an exception; if the formatting cannot be done, the
+    returned string will describe the event so that a useful
+    message is emitted regardless.
+
+    @param event: a logging event
+
+    @return: a L{unicode}
+    """
+    try:
+        format = event.get("log_format", None)
+
+        if format is None:
+            raise ValueError("No log format provided")
+
+        # Make sure format is unicode.
+        if type(format) is bytes:
+            # If we get bytes, assume it's UTF-8 bytes
+            format = format.decode("utf-8")
+        else:
+            # For anything else, assume we can just convert to unicode
+            format = unicode(format)
+
+        return formatWithCall(format, event)
+
+    except BaseException as e:
+        try:
+            return formatUnformattableEvent(event, e)
+        except:
+            return u"MESSAGE LOST"
+
+
+def formatUnformattableEvent(event, error):
+    """
+    Formats an event as a L{unicode} that describes the event
+    generically and a formatting error.
+
+    @param event: a logging event
+
+    @param error: the formatting error
+
+    @return: a L{unicode}
+    """
+    try:
+        return (
+            u"Unable to format event {event}: {error}"
+            .format(event=event, error=error)
+        )
+    except BaseException 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:
+                    items.append(u"<UNFORMATTABLE KEY> = ")
+                try:
+                    items.append(u"{value}".format(value=value))
+                except:
+                    items.append(u"<UNFORMATTABLE VALUE>")
+
+            text = ", ".join(items)
+        except:
+            text = ""
+
+        return (
+            u"MESSAGE LOST: Unformattable object logged: {error}\n"
+            u"Recoverable data: {text}"
+            .format(text=text)
+        )
+
+
+
 class Logger(object):
     """
     Logging object.
@@ -257,90 +342,6 @@
         return "<%s %r>" % (self.__class__.__name__, self.namespace)
 
 
-    @classmethod
-    def formatEvent(cls, event):
-        """
-        Formats an event as a L{unicode}, using the format in
-        C{event["log_format"]}.  This implementation should never
-        raise an exception; if the formatting cannot be done, the
-        returned string will describe the event so that a useful
-        message is emitted regardless.
-
-        @param event: a logging event
-
-        @return: a L{unicode}
-        """
-        try:
-            format = event.get("log_format", None)
-
-            if format is None:
-                raise ValueError("No log format provided")
-
-            # Make sure format is unicode.
-            if type(format) is bytes:
-                # If we get bytes, assume it's UTF-8 bytes
-                format = format.decode("utf-8")
-            else:
-                # For anything else, assume we can just convert to unicode
-                format = unicode(format)
-
-            return formatWithCall(format, event)
-
-        except BaseException as e:
-            try:
-                return cls.formatUnformattableEvent(event, e)
-            except:
-                return u"MESSAGE LOST"
-
-
-    @classmethod
-    def formatUnformattableEvent(cls, event, error):
-        """
-        Formats an event as a L{unicode} that describes the event
-        generically and a formatting error.
-
-        @param event: a logging event
-
-        @param error: the formatting error
-
-        @return: a L{unicode}
-        """
-        try:
-            return (
-                u"Unable to format event {event}: {error}"
-                .format(event=event, error=error)
-            )
-        except BaseException 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:
-                        items.append(u"<UNFORMATTABLE KEY> = ")
-                    try:
-                        items.append(u"{value}".format(value=value))
-                    except:
-                        items.append(u"<UNFORMATTABLE VALUE>")
-
-                text = ", ".join(items)
-            except:
-                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.
@@ -639,16 +640,14 @@
             # legacy log observer.
             #
             class LegacyFormatStub(object):
-                def __str__(self):
-                    return self.formatEvent(event).encode("utf-8")
+                def __str__(oself):
+                    return formatEvent(event).encode("utf-8")
 
             event["format"] = prefix + "%(log_legacy)s"
             event["log_legacy"] = LegacyFormatStub()
 
         # log.failure() -> isError blah blah
         if "log_failure" in event:
-            formatEvent = event.get("log_logger", Logger).formatEvent
-
             event["failure"] = event["log_failure"]
             event["isError"] = 1
             event["why"] = "{prefix}{message}".format(prefix=prefix, message=formatEvent(event))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130613/b8569829/attachment.html>


More information about the calendarserver-changes mailing list