Use python logging in install / log cherrypy at right severity
[cascardo/ipsilon.git] / ipsilon / util / log.py
index 0a7a9df..ae851af 100644 (file)
@@ -7,6 +7,7 @@ import cStringIO
 import inspect
 import os
 import traceback
+import logging
 
 
 def log_request_response():
@@ -136,6 +137,21 @@ def log_request_response():
         f.close()
         return string
 
+    def print_param(name, value):
+        f = cStringIO.StringIO()
+
+        # Might be a multipart Part object, if so format it
+        if isinstance(value, cherrypy._cpreqbody.Part):  # pylint:disable=W0212
+            f.write(indent_text("%s:\n" % (name)))
+            f.write(indent_text(print_part(value), 1))
+        else:
+            # Not a mulitpart, just write it as a string
+            f.write(indent_text("%s: %s\n" % (name, value)))
+
+        string = f.getvalue()
+        f.close()
+        return string
+
     def collapse_body(body):
         '''The cherrypy response body can be:
 
@@ -197,17 +213,10 @@ def log_request_response():
             # Multi-valued paramater is in a list
             if isinstance(value, list):
                 for i, item in enumerate(value):
-                    # Might be a multipart Part object, if so format it
-                    if getattr(item, "part_class", None):
-                        f.write(indent_text("%s[%s]:\n" % (name, i), 2))
-                        f.write(indent_text(print_part(item), 3))
-                    else:
-                        # Not a mulitpart, just write it as a string
-                        f.write(indent_text("%s[%s]: %s\n" %
-                                            (name, i, item), 2))
+                    f.write(indent_text(print_param("%s[%d]" % (name, i),
+                                                    item), 2))
             else:
-                # Just a string value
-                f.write(indent_text("%s: %s\n" % (name, value), 2))
+                f.write(indent_text(print_param(name, value), 2))
 
     # If the body is multipart format each of the parts
     if request.body.parts:
@@ -319,6 +328,6 @@ class Log(object):
         cherrypy.log(fact)
 
     def error(self, fact):
-        cherrypy.log.error('ERROR: %s' % fact)
+        cherrypy.log.error('ERROR: %s' % fact, severity=logging.ERROR)
         if cherrypy.config.get('stacktrace_on_error', False):
             cherrypy.log.error(Log.stacktrace())