Set up celery logging from paste ini
authorJoar Wandborg <git@wandborg.com>
Sun, 30 Sep 2012 10:43:36 +0000 (12:43 +0200)
committerJoar Wandborg <git@wandborg.com>
Sun, 30 Sep 2012 10:43:36 +0000 (12:43 +0200)
mediagoblin/init/celery/from_celery.py

index b829725525eb5ec69761c0758891200bcf72bb9d..01da6b32cdcdce0cd3cf78f3693e227f5340e0f1 100644 (file)
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 import os
+import logging
+
+from configobj import ConfigObj
+from celery.signals import setup_logging
 
 from mediagoblin import app, mg_globals
 from mediagoblin.init.celery import setup_celery_from_config
@@ -22,6 +26,67 @@ from mediagoblin.init.celery import setup_celery_from_config
 
 OUR_MODULENAME = __name__
 
+logging.basicConfig()
+_log = logging.getLogger(__name__)
+_log.setLevel(logging.DEBUG)
+_log.warning('Test')
+
+
+def setup_logging_from_paste_ini(*args, **kw):
+    if os.path.exists(os.path.abspath('paste_local.ini')):
+        logging_conf_file = 'paste_local.ini'
+    else:
+        logging_conf_file = 'paste.ini'
+
+    if not os.path.exists(logging_conf_file):
+        raise IOError('{0} does not exist. Logging can not be set up.'.format(
+            logging_conf_file))
+
+    logging_conf = ConfigObj(logging_conf_file)
+
+    config = logging_conf
+
+    # Set up formatting
+    # Get the format string and circumvent configobj interpolation of the value
+    fmt = config['formatter_generic'].viewitems().__iter__()\
+            .next()[1]
+
+    # Create the formatter
+    formatter = logging.Formatter(fmt)
+
+    # Check for config values
+    if not config.get('loggers') or not config['loggers'].get('keys'):
+        print('No loggers found')
+        return
+
+    # Iterate all teh loggers.keys values
+    for name in config['loggers']['keys'].split(','):
+        if not config.get('logger_{0}'.format(name)):
+            continue
+
+        log_params = config['logger_{0}'.format(name)]
+
+        qualname = log_params['qualname'] if 'qualname' in log_params else name
+
+        if qualname == 'root':
+            qualname = None
+
+        logger = logging.getLogger(qualname)
+
+        level = getattr(logging, log_params['level'])
+        logger.setLevel(level)
+
+        for handler in logger.handlers:
+            handler.setFormatter(formatter)
+
+        print('Logger {0} set to level {1} ({2})'.format(
+            qualname,
+            log_params['level'],
+            level))
+
+
+setup_logging.connect(setup_logging_from_paste_ini)
+
 
 def setup_self(check_environ_for_conf=True, module_name=OUR_MODULENAME,
                default_conf_file=None):