Rev 413: Bring in configurable-logging in http://bazaar.launchpad.net/~jameinel/loggerhead/all

John Arbash Meinel john at arbash-meinel.com
Mon Apr 26 22:29:40 BST 2010


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

------------------------------------------------------------
revno: 413 [merge]
revision-id: john at arbash-meinel.com-20100426212902-nb76zvp1h8qcee90
parent: john at arbash-meinel.com-20100426212836-hmdswqil8zh528xy
parent: john at arbash-meinel.com-20100426193726-qfhu1t3pn7u1kq1g
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: all
timestamp: Mon 2010-04-26 16:29:02 -0500
message:
  Bring in configurable-logging
modified:
  __init__.py                    __init__.py-20090123174919-ekabddqbmvwuci2i-1
  loggerhead/apps/branch.py      wsgiapp.py-20080612051902-5y0zpdi6fhxgun6z-2
  loggerhead/config.py           config.py-20090331163436-x6habdrjtdq2pvxg-1
  loggerhead/controllers/__init__.py __init__.py-20061211064342-102iqirsciyvgtcf-17
  loggerhead/main.py             servebranches.py-20080618011543-s6ydx5c3bl38zap6-1
  loggerhead/wholehistory.py     graphcache.py-20080630085128-vjt0vaigp70he531-1
-------------- 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-26 18:17:26 +0000
+++ b/loggerhead/apps/branch.py	2010-04-26 21:29:02 +0000
@@ -19,6 +19,7 @@
 import logging
 import urllib
 import sys
+import time
 
 import bzrlib.branch
 import bzrlib.errors
@@ -158,23 +159,33 @@
                     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(
                 self._url_base + '/changes')
         if path == 'static':
+            # TODO: Unfortunately, we still call Branch.open() even if we are
+            #       serving a static path. This probably adds a fair amount of
+            #       overhead...
             return static_app(environ, start_response)
         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 to generate: %s' % (tdelta, path_info))
+        return val

=== modified file 'loggerhead/config.py'
--- a/loggerhead/config.py	2010-04-26 18:17:26 +0000
+++ b/loggerhead/config.py	2010-04-26 21:29:02 +0000
@@ -52,6 +52,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",
@@ -81,6 +87,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."""
 
@@ -108,6 +137,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/controllers/__init__.py'
--- a/loggerhead/controllers/__init__.py	2009-10-17 08:59:33 +0000
+++ b/loggerhead/controllers/__init__.py	2010-04-26 19:37:26 +0000
@@ -92,7 +92,7 @@
 
         vals.update(self.get_values(path, kwargs, headers))
 
-        self.log.info('Getting information for %s: %r secs' % (
+        self.log.info('Getting information for %s: %.3f secs' % (
             self.__class__.__name__, time.time() - z))
         if 'Content-Type' not in headers:
             headers['Content-Type'] = 'text/html'
@@ -103,7 +103,7 @@
         template.expand_into(w, **vals)
         w.flush()
         self.log.info(
-            'Rendering %s: %r secs, %s bytes' % (
+            'Rendering %s: %.3f secs, %s bytes' % (
                 self.__class__.__name__, time.time() - z, w.bytes))
         return []
 

=== 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')

=== modified file 'loggerhead/wholehistory.py'
--- a/loggerhead/wholehistory.py	2009-10-21 14:40:23 +0000
+++ b/loggerhead/wholehistory.py	2010-04-26 19:37:26 +0000
@@ -80,6 +80,6 @@
             if revid not in c[1]:
                 c[1] = c[1] + (revid,)
 
-    log.info('built revision graph cache: %r secs' % (time.time() - z,))
+    log.info('built revision graph cache: %.3f secs' % (time.time() - z,))
 
     return (_rev_info, _rev_indices)



More information about the bazaar-commits mailing list