[CalendarServer-changes] [4327] CalendarServer/trunk/twistedcaldav

source_changes at macosforge.org source_changes at macosforge.org
Thu Jun 4 12:21:56 PDT 2009


Revision: 4327
          http://trac.macosforge.org/projects/calendarserver/changeset/4327
Author:   wsanchez at apple.com
Date:     2009-06-04 12:21:56 -0700 (Thu, 04 Jun 2009)
Log Message:
-----------
Send logLevel kwarg to log.msg

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

Modified: CalendarServer/trunk/twistedcaldav/log.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/log.py	2009-06-04 19:20:49 UTC (rev 4326)
+++ CalendarServer/trunk/twistedcaldav/log.py	2009-06-04 19:21:56 UTC (rev 4327)
@@ -48,6 +48,7 @@
     "cmpLogLevels",
     "lowestLogLevel",
     "highestLogLevel",
+    "pythonLogLevelForLevel",
     "logLevelForNamespace",
     "setLogLevelForNamespace",
     "clearLogLevels",
@@ -58,6 +59,7 @@
 ]
 
 import inspect
+import logging
 
 from twisted.python import log
 
@@ -79,6 +81,13 @@
 logLevelIndexes = dict(zip(logLevels, xrange(0, len(logLevels))))
 
 def cmpLogLevels(a, b):
+    """
+    Compare two log levels.
+    @param a: a log level
+    @param b: a log level
+    @return: a negative integer if C{a < b}, C{0} if C{a == b}, or a
+        positive integer if C{a > b}.
+    """
     return cmp(logLevelIndexes[a], logLevelIndexes[b])
 
 def lowestLogLevel(*levels):
@@ -88,6 +97,38 @@
     return sorted(levels, cmpLogLevels, reverse=True)[0]
 
 ##
+# Mappings to Python's logging module
+##
+
+pythonLogLevelMapping = {
+    "debug"   : logging.DEBUG,
+    "info"    : logging.INFO,
+    "warn"    : logging.WARNING,
+    "error"   : logging.ERROR,
+   #"critical": logging.CRITICAL,
+}
+
+def pythonLogLevelForLevel(level):
+    """
+    @param: a log level
+    @return: a L{logging} module log level
+    """
+    if level in pythonLogLevelMapping:
+        return pythonLogLevelMapping[level]
+
+    raise InvalidLogLevelError(level)
+
+#    #
+#    # In case we add log levels that don't map to pythong logging levels:
+#    #
+#    for l in logLevels:
+#        print "Trying %s: %s, %s" % (l, l in pythonLogLevelMapping, cmpLogLevels(level, l) <= 0)
+#        if l in pythonLogLevelMapping and cmpLogLevels(level, l) <= 0:
+#            return pythonLogLevelMapping[l]
+#
+#    return logging.CRITICAL
+
+##
 # Tools for managing log levels
 ##
 
@@ -178,6 +219,7 @@
                 "[%s#%s] %s" % (self.namespace, level, message),
                 isError = (cmpLogLevels(level, "error") >= 0),
                 level = level,
+                logLevel = pythonLogLevelForLevel(level),
                 namespace = self.namespace,
                 **kwargs
             )

Modified: CalendarServer/trunk/twistedcaldav/test/test_log.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/test/test_log.py	2009-06-04 19:20:49 UTC (rev 4326)
+++ CalendarServer/trunk/twistedcaldav/test/test_log.py	2009-06-04 19:21:56 UTC (rev 4327)
@@ -14,6 +14,8 @@
 # limitations under the License.
 ##
 
+import logging
+
 from twistedcaldav.log import *
 from twistedcaldav.test.util import TestCase
 
@@ -51,6 +53,14 @@
         self.assertEquals(highestLogLevel(*reversed(logLevels)), "error")
         self.assertEquals(highestLogLevel("warn", "info"), "warn")
 
+    def test_pythonLogLevel(self):
+        self.assertEquals(pythonLogLevelForLevel("debug"), logging.DEBUG)
+        self.assertEquals(pythonLogLevelForLevel("info"), logging.INFO)
+        self.assertEquals(pythonLogLevelForLevel("warn"), logging.WARNING)
+        self.assertEquals(pythonLogLevelForLevel("error"), logging.ERROR)
+        #self.assertEquals(pythonLogLevelForLevel("critical"), logging.CRITICAL)
+        self.assertRaises(InvalidLogLevelError, pythonLogLevelForLevel, "-not-a-log-level-")
+
     def test_namespace_default(self):
         """
         Default namespace is module name.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20090604/852a5c1f/attachment.html>


More information about the calendarserver-changes mailing list