Rev 435: Re-land the configurable_logging changes, with some code simplification from Jelmer. in http://bazaar.launchpad.net/~loggerhead-team/loggerhead/trunk-rich

John Arbash Meinel john at arbash-meinel.com
Wed Mar 16 12:20:45 UTC 2011


At http://bazaar.launchpad.net/~loggerhead-team/loggerhead/trunk-rich

------------------------------------------------------------
revno: 435 [merge]
revision-id: john at arbash-meinel.com-20110316122028-tgixpjm30aalqk73
parent: john at arbash-meinel.com-20110315102440-n4zr988738z125cd
parent: john at arbash-meinel.com-20110316114005-1rdrmdu9hda30xak
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: trunk-rich
timestamp: Wed 2011-03-16 13:20:28 +0100
message:
  Re-land the configurable_logging changes, with some code simplification from Jelmer.
modified:
  __init__.py                    __init__.py-20090123174919-ekabddqbmvwuci2i-1
  loggerhead/apps/transport.py   wsgitest.py-20080612051902-5y0zpdi6fhxgun6z-1
  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
  loggerhead/zptsupport.py       zptsupport.py-20070629104744-hxhwg7x016jnmjit-7
-------------- next part --------------
=== modified file '__init__.py'
--- a/__init__.py	2011-02-10 01:57:32 +0000
+++ b/__init__.py	2011-03-16 11:40:05 +0000
@@ -49,17 +49,6 @@
     HELP = ('Loggerhead, a web-based code viewer and server. (default port: %d)' %
             (DEFAULT_PORT,))
 
-    def setup_logging(config):
-        import logging
-        import sys
-
-        logger = logging.getLogger('loggerhead')
-        handler = logging.StreamHandler(sys.stderr)
-        handler.setLevel(logging.DEBUG)
-        logger.addHandler(handler)
-        logging.getLogger('simpleTAL').addHandler(handler)
-        logging.getLogger('simpleTALES').addHandler(handler)
-
     def _ensure_loggerhead_path():
         """Ensure that you can 'import loggerhead' and get the root."""
         # loggerhead internal code will try to 'import loggerhead', so
@@ -78,6 +67,7 @@
 
         from loggerhead.apps.transport import BranchesFromTransportRoot
         from loggerhead.config import LoggerheadConfig
+        from loggerhead.main import setup_logging
 
         if host is None:
             host = DEFAULT_HOST
@@ -87,7 +77,7 @@
         if not transport.is_readonly():
             argv.insert(0, '--allow-writes')
         config = LoggerheadConfig(argv)
-        setup_logging(config)
+        setup_logging(config, init_logging=False, log_file=sys.stderr)
         app = BranchesFromTransportRoot(transport.base, config)
         app = HTTPExceptionHandler(app)
         serve(app, host=host, port=port)

=== modified file 'loggerhead/apps/transport.py'
--- a/loggerhead/apps/transport.py	2010-05-05 18:35:35 +0000
+++ b/loggerhead/apps/transport.py	2011-03-16 11:40:05 +0000
@@ -1,4 +1,4 @@
-# Copyright (C) 2008, 2009 Canonical Ltd.
+# Copyright (C) 2008-2011 Canonical Ltd.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by

=== modified file 'loggerhead/config.py'
--- a/loggerhead/config.py	2010-04-13 23:35:01 +0000
+++ b/loggerhead/config.py	2010-05-12 14:38:05 +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):
+    parser.values.log_level = _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/controllers/__init__.py'
--- a/loggerhead/controllers/__init__.py	2011-02-10 17:01:10 +0000
+++ b/loggerhead/controllers/__init__.py	2011-03-16 11:40:05 +0000
@@ -94,7 +94,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'
@@ -108,7 +108,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	2011-03-16 11:39:27 +0000
@@ -56,21 +56,39 @@
     return config, base
 
 
-def setup_logging(config):
-    logging.basicConfig()
-    logging.getLogger('').setLevel(logging.DEBUG)
+def setup_logging(config, init_logging=True, log_file=None):
+    log_level = config.get_log_level()
+    if init_logging:
+        logging.basicConfig()
+        if log_level is not None:
+            logging.getLogger('').setLevel(log_level)
     logger = logging.getLogger('loggerhead')
-    if config.get_option('log_folder'):
-        logfile_path = os.path.join(
-            config.get_option('log_folder'), 'serve-branches.log')
+    if log_level is not None:
+        logger.setLevel(log_level)
+    if log_file is not None:
+        handler = logging.StreamHandler(log_file)
     else:
-        logfile_path = 'serve-branches.log'
-    logfile = logging.FileHandler(logfile_path, 'a')
-    formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(name)s:'
-                                  ' %(message)s')
-    logfile.setFormatter(formatter)
-    logfile.setLevel(logging.DEBUG)
-    logger.addHandler(logfile)
+        if config.get_option('log_folder'):
+            logfile_path = os.path.join(
+                config.get_option('log_folder'), 'serve-branches.log')
+        else:
+            logfile_path = 'serve-branches.log'
+        handler = logging.FileHandler(logfile_path, 'a')
+        formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(name)s:'
+                                      ' %(message)s')
+        handler.setFormatter(formatter)
+    # We set the handler to accept all messages, the *logger* won't emit them
+    # if it is configured to suppress it
+    handler.setLevel(logging.DEBUG)
+    logger.addHandler(handler)
+    def _restrict_logging(logger_name):
+        logger = logging.getLogger(logger_name)
+        if logger.getEffectiveLevel() < logging.INFO:
+            logger.setLevel(logging.INFO)
+    # simpleTAL is *very* verbose in DEBUG mode, which is otherwise the
+    # default. So quiet it up a bit.
+    _restrict_logging('simpleTAL')
+    _restrict_logging('simpleTALES')
     return logger
 
 

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

=== modified file 'loggerhead/zptsupport.py'
--- a/loggerhead/zptsupport.py	2009-03-02 05:44:20 +0000
+++ b/loggerhead/zptsupport.py	2010-05-12 15:01:44 +0000
@@ -23,10 +23,6 @@
 
 from simpletal import simpleTAL, simpleTALES
 
-logging.getLogger("simpleTAL").setLevel(logging.INFO)
-logging.getLogger("simpleTALES").setLevel(logging.INFO)
-
-
 _zpt_cache = {}
 
 



More information about the bazaar-commits mailing list