Rev 418: Remove duplicated effort in setup_logging. in http://bazaar.launchpad.net/~jameinel/loggerhead/configurable_logging

John Arbash Meinel john at arbash-meinel.com
Wed Mar 16 11:35:11 UTC 2011


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

------------------------------------------------------------
revno: 418
revision-id: john at arbash-meinel.com-20110316113503-c21ve1v3wru7kvae
parent: john at arbash-meinel.com-20100512150144-vfxru5xvfqldwcbm
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: configurable_logging
timestamp: Wed 2011-03-16 12:35:03 +0100
message:
  Remove duplicated effort in setup_logging.
  
  Instead, pass in some extra flags, indicating what loggerhead should do.
-------------- next part --------------
=== modified file '__init__.py'
--- a/__init__.py	2010-05-12 15:01:44 +0000
+++ b/__init__.py	2011-03-16 11:35:03 +0000
@@ -1,4 +1,4 @@
-# Copyright 2009, 2010 Canonical Ltd
+# Copyright 2009, 2010, 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
@@ -58,30 +58,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')
-        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)
-        logging.getLogger('simpleTAL').addHandler(handler)
-        logging.getLogger('simpleTALES').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')
-
-
-
     def serve_http(transport, host=None, port=None, inet=None):
         from paste.httpexceptions import HTTPExceptionHandler
         from paste.httpserver import serve
@@ -96,6 +72,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
@@ -105,7 +82,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/main.py'
--- a/loggerhead/main.py	2010-05-12 15:01:44 +0000
+++ b/loggerhead/main.py	2011-03-16 11:35:03 +0000
@@ -56,23 +56,31 @@
     return config, base
 
 
-def setup_logging(config):
-    logging.basicConfig()
+def setup_logging(config, init_logging=True, log_file=None):
     log_level = config.get_log_level()
-    logging.getLogger('').setLevel(log_level)
+    if init_logging:
+        logging.basicConfig()
+        if log_level is not None:
+            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')
+    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')
+        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')
-    logfile.setFormatter(formatter)
-    logfile.setLevel(logging.DEBUG)
-    logger.addHandler(logfile)
+    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:



More information about the bazaar-commits mailing list