Robert Brewer avatar Robert Brewer committed 2650154

Audit of logging:

1. cherrypy.log now defaults to INFO.
2. Bus.log now takes a 'level=INFO' arg.
3. 'log' channel subscribers now must accept a 'level' arg.
4. Some bus plugin messages upgraded to WARN or ERROR.
5. Some CP core messages upgraded to ERROR.
6. log.screen move to stderr polished. See #747.

Comments (0)

Files changed (10)

cherrypy/__init__.py

 # Using an access file makes CP about 10% slower. Leave off by default.
 log.access_file = ''
 
-def _buslog(msg):
-    log.error(msg, 'ENGINE')
+def _buslog(msg, level):
+    log.error(msg, 'ENGINE', severity=level)
 engine.subscribe('log', _buslog)
 
 #                       Helper functions for CP apps                       #

cherrypy/_cplogging.py

                     h.stream = open(h.baseFilename, h.mode)
                     h.release()
     
-    def error(self, msg='', context='', severity=logging.DEBUG, traceback=False):
+    def error(self, msg='', context='', severity=logging.INFO, traceback=False):
         """Write to the error log.
         
         This is not just for errors! Applications may call this at any time
         if enable:
             if not h:
                 if stream is None:
-                    stream=sys.stdout
+                    stream=sys.stderr
                 h = logging.StreamHandler(stream)
                 h.setLevel(logging.DEBUG)
                 h.setFormatter(logfmt)
         self._set_screen_handler(self.error_log, newvalue, stream=sys.stderr)
         self._set_screen_handler(self.access_log, newvalue)
     screen = property(_get_screen, _set_screen,
-                      doc="If True, error and access will print to stdout.")
+                      doc="If True, error and access will print to stderr.")
     
     
     # -------------------------- File handlers -------------------------- #

cherrypy/_cprequest.py

                     exc = sys.exc_info()[1]
                 except:
                     exc = sys.exc_info()[1]
-                    cherrypy.log(traceback=True)
+                    cherrypy.log(traceback=True, severity=40)
         if exc:
             raise
     
             else:
                 # Failure in setup, error handler or finalize. Bypass them.
                 # Can't use handle_error because we may not have hooks yet.
-                cherrypy.log(traceback=True)
+                cherrypy.log(traceback=True, severity=40)
                 if self.show_tracebacks:
                     body = format_exc()
                 else:

cherrypy/_cptree.py

         try:
             req.close()
         except:
-            cherrypy.log(traceback=True)
+            cherrypy.log(traceback=True, severity=40)
         
         cherrypy.serving.clear()
     

cherrypy/_cpwsgi.py

                 raise
             
             tb = _cperror.format_exc()
-            _cherrypy.log(tb)
+            _cherrypy.log(tb, severity=40)
             if not getattr(self.request, "show_tracebacks", True):
                 tb = ""
             s, h, b = _cperror.bare_error(tb)
                 self.close()
                 raise
             
-            _cherrypy.log(traceback=True)
+            _cherrypy.log(traceback=True, severity=40)
             self.close()
             
             # CherryPy test suite expects bare_error body to be output,
                 self.close()
                 raise
             
-            _cherrypy.log(traceback=True)
+            _cherrypy.log(traceback=True, severity=40)
             
             # CherryPy test suite expects bare_error body to be output,
             # so don't call start_response (which, according to PEP 333,

cherrypy/lib/cptools.py

 
 def log_traceback(severity=logging.DEBUG):
     """Write the last error's traceback to the cherrypy error log."""
-    from cherrypy import _cperror
-    cherrypy.log(_cperror.format_exc(), "HTTP", severity=severity)
+    cherrypy.log("", "HTTP", severity=severity, traceback=True)
 
 def log_request_headers():
     """Write request headers to the cherrypy error log."""

cherrypy/process/plugins.py

     def _set_uid(self, val):
         if val is not None:
             if pwd is None:
-                self.bus.log("pwd module not available; ignoring uid.")
+                self.bus.log("pwd module not available; ignoring uid.",
+                             level=30)
                 val = None
             elif isinstance(val, basestring):
                 val = pwd.getpwnam(val)[2]
     def _set_gid(self, val):
         if val is not None:
             if grp is None:
-                self.bus.log("grp module not available; ignoring gid.")
+                self.bus.log("grp module not available; ignoring gid.",
+                             level=30)
                 val = None
             elif isinstance(val, basestring):
                 val = grp.getgrnam(val)[2]
             try:
                 os.umask
             except AttributeError:
-                self.bus.log("umask function not available; ignoring umask.")
+                self.bus.log("umask function not available; ignoring umask.",
+                             level=30)
                 val = None
         self._umask = val
     umask = property(_get_umask, _set_umask, doc="The umask under which to run.")
         else:
             if self.uid is None and self.gid is None:
                 if pwd or grp:
-                    self.bus.log('uid/gid not set')
+                    self.bus.log('uid/gid not set', level=30)
             else:
                 self.bus.log('Started as uid: %r gid: %r' % current_ids())
                 if self.gid is not None:
                 self.bus.log('umask already set to: %03o' % self.umask)
         else:
             if self.umask is None:
-                self.bus.log('umask not set')
+                self.bus.log('umask not set', level=30)
             else:
                 old_umask = os.umask(self.umask)
                 self.bus.log('umask old: %03o, new: %03o' %
         if threading.activeCount() != 1:
             self.bus.log('There are %r active threads. '
                          'Daemonizing now may cause strange failures.' %
-                         threading.enumerate())
+                         threading.enumerate(), level=30)
         
         # See http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
         # (or http://www.faqs.org/faqs/unix-faq/programmer/faq/ section 1.7)

cherrypy/process/servers.py

             import sys
             self.interrupt = sys.exc_info()[1]
             self.bus.log("Error in HTTP server: shutting down",
-                         traceback=True)
+                         traceback=True, level=40)
             self.bus.exit()
             raise
     

cherrypy/process/win32.py

         result = win32api.SetConsoleCtrlHandler(self.handle, 1)
         if result == 0:
             self.bus.log('Could not SetConsoleCtrlHandler (error %r)' %
-                         win32api.GetLastError())
+                         win32api.GetLastError(), level=40)
         else:
             self.is_set = True
     
         
         if result == 0:
             self.bus.log('Could not remove SetConsoleCtrlHandler (error %r)' %
-                         win32api.GetLastError())
+                         win32api.GetLastError(), level=40)
         else:
             self.is_set = False
     

cherrypy/process/wspbus.py

                 raise
             except:
                 self.log("Error in %r listener %r" % (channel, listener),
-                         traceback=True)
+                         level=40, traceback=True)
                 exc = sys.exc_info()[1]
         if exc:
             raise
         except (KeyboardInterrupt, SystemExit):
             raise
         except:
-            self.log("Shutting down due to error in start listener:\n%s" %
-                     _traceback.format_exc())
+            self.log("Shutting down due to error in start listener:",
+                     level=40, traceback=True)
             e_info = sys.exc_info()
             try:
                 self.exit()
         
         return t
     
-    def log(self, msg="", traceback=False):
+    def log(self, msg="", level=20, traceback=False):
         """Log the given message. Append the last traceback if requested."""
         if traceback:
             exc = sys.exc_info()
             msg += "\n" + "".join(_traceback.format_exception(*exc))
-        self.publish('log', msg)
+        self.publish('log', msg, level)
 
 bus = Bus()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.