Rev 412: Add 'http_log_level' and --log-level. in http://bazaar.launchpad.net/~jameinel/loggerhead/configurable_logging

John Arbash Meinel john at arbash-meinel.com
Mon Apr 26 20:04:44 BST 2010


At http://bazaar.launchpad.net/~jameinel/loggerhead/configurable_logging

------------------------------------------------------------
revno: 412
revision-id: john at arbash-meinel.com-20100426190406-psgsbys8e33cpcbg
parent: mnordhoff at mattnordhoff.com-20100424124017-do5wupmzlkmejtvk
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: configurable_logging
timestamp: Mon 2010-04-26 14:04:06 -0500
message:
  Add 'http_log_level' and --log-level.
  
  This allows us to set a default log level that isn't 'WARNING' the logger
  default, and isn't DEBUG, the current code default.
  
  
  Also add a time() check for serving a particular branch page.
-------------- next part --------------
=== modified file '__init__.py'
--- a/__init__.py	2010-04-22 08:52:59 +0000
+++ b/__init__.py	2010-04-26 19:04:06 +0000
@@ -63,6 +63,9 @@
         import sys
 
         logger = logging.getLogger('loggerhead')
+        log_level = config.get_log_level()
+        if log_level is not None:
+            logger.setLevel(log_level)
         handler = logging.StreamHandler(sys.stderr)
         handler.setLevel(logging.DEBUG)
         logger.addHandler(handler)

=== modified file 'loggerhead/apps/branch.py'
--- a/loggerhead/apps/branch.py	2010-04-14 17:54:32 +0000
+++ b/loggerhead/apps/branch.py	2010-04-26 19:04:06 +0000
@@ -19,6 +19,7 @@
 import logging
 import urllib
 import sys
+import time
 
 import bzrlib.branch
 import bzrlib.errors
@@ -151,6 +152,7 @@
                     self.served_url = self.url([])
                 except bzrlib.errors.InvalidURL:
                     self.served_url = None
+        path_info = environ.get('PATH_INFO', None)
         path = request.path_info_pop(environ)
         if not path:
             raise httpexceptions.HTTPMovedPermanently(
@@ -160,14 +162,20 @@
         cls = self.controllers_dict.get(path)
         if cls is None:
             raise httpexceptions.HTTPNotFound()
-        self.branch.lock_read()
-        try:
+        def do_stuff():
+            self.branch.lock_read()
             try:
-                c = cls(self, self.get_history)
-                return c(environ, start_response)
-            except:
-                environ['exc_info'] = sys.exc_info()
-                environ['branch'] = self
-                raise
-        finally:
-            self.branch.unlock()
+                try:
+                    c = cls(self, self.get_history)
+                    return c(environ, start_response)
+                except:
+                    environ['exc_info'] = sys.exc_info()
+                    environ['branch'] = self
+                    raise
+            finally:
+                self.branch.unlock()
+        t = time.time()
+        val = do_stuff()
+        tdelta = time.time() - t
+        self.log.info('Took %.3fs for: %s' % (tdelta, path_info))
+        return val

=== modified file 'loggerhead/config.py'
--- a/loggerhead/config.py	2010-04-13 23:35:01 +0000
+++ b/loggerhead/config.py	2010-04-26 19:04:06 +0000
@@ -49,6 +49,12 @@
     parser.add_option("--protocol", dest="protocol",
                       help=("Protocol to use: http, scgi, fcgi, ajp"
                            "(defaults to http)."))
+    parser.add_option("--log-level", default=None, action='callback',
+                      callback=_optparse_level_to_int_level,
+                      type="string",
+                      help="Set the verbosity of logging. Can either"
+                           " be set to a numeric or string"
+                           " (eg, 10=debug, 30=warning)")
     parser.add_option("--memory-profile", action="store_true",
                       help="Profile the memory usage using Dozer.")
     parser.add_option("--prefix", dest="user_prefix",
@@ -72,6 +78,29 @@
     return parser
 
 
+_log_levels = {
+    'debug': 10,
+    'info': 20,
+    'warning': 30,
+    'error': 40,
+    'critical': 50,
+}
+
+def _optparse_level_to_int_level(option, opt_str, value, parser):
+    return _level_to_int_level(value)
+
+
+def _level_to_int_level(value):
+    """Convert a string level to an integer value."""
+    if value is None:
+        return None
+    try:
+        return int(value)
+    except ValueError:
+        pass
+    return _log_levels[value.lower()]
+
+
 class LoggerheadConfig(object):
     """A configuration object."""
 
@@ -99,6 +128,10 @@
         else:
             return cmd_config
 
+    def get_log_level(self):
+        opt = self.get_option('log_level')
+        return _level_to_int_level(opt)
+
     def get_arg(self, index):
         """Get an arg from the arg list."""
         return self._args[index]

=== modified file 'loggerhead/main.py'
--- a/loggerhead/main.py	2009-11-27 18:30:24 +0000
+++ b/loggerhead/main.py	2010-04-26 19:04:06 +0000
@@ -58,8 +58,10 @@
 
 def setup_logging(config):
     logging.basicConfig()
-    logging.getLogger('').setLevel(logging.DEBUG)
+    log_level = config.get_log_level()
+    logging.getLogger('').setLevel(log_level)
     logger = logging.getLogger('loggerhead')
+    logger.setLevel(log_level)
     if config.get_option('log_folder'):
         logfile_path = os.path.join(
             config.get_option('log_folder'), 'serve-branches.log')



More information about the bazaar-commits mailing list