From: Joar Wandborg Date: Sun, 30 Sep 2012 10:43:36 +0000 (+0200) Subject: Set up celery logging from paste ini X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=b1bb15d1f9f946a328d1a90320522ea237d770e8;p=mediagoblin.git Set up celery logging from paste ini --- diff --git a/mediagoblin/init/celery/from_celery.py b/mediagoblin/init/celery/from_celery.py index b8297255..01da6b32 100644 --- a/mediagoblin/init/celery/from_celery.py +++ b/mediagoblin/init/celery/from_celery.py @@ -15,6 +15,10 @@ # along with this program. If not, see . 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):