From 37b3bb4bd4442da21084916d749a4ff8f6dae17a Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Fri, 18 Jan 2013 15:42:18 +0100 Subject: [PATCH] Have Worker.work() setup logging. Basically, I don't want to enforce users to configure _any_ logging stack when writing a basic worker, like this: from rq import Worker, Queue, Connection with Connection(): q = Queue() w = Worker([q]) w.work(burst=True) In case you want to disable logging altogether, you can configure your logging stack to do so. --- rq/logutils.py | 31 +++++++++++++++++++++++++++++++ rq/scripts/rqworker.py | 33 ++------------------------------- rq/worker.py | 2 ++ 3 files changed, 35 insertions(+), 31 deletions(-) create mode 100644 rq/logutils.py diff --git a/rq/logutils.py b/rq/logutils.py new file mode 100644 index 0000000..05cfc2b --- /dev/null +++ b/rq/logutils.py @@ -0,0 +1,31 @@ +import logging.config + + +def setup_loghandlers(verbose=False): + if not logging._handlers: + logging.config.dictConfig({ + "version": 1, + "disable_existing_loggers": False, + + "formatters": { + "console": { + "format": "%(asctime)s %(message)s", + "datefmt": "%H:%M:%S", + }, + }, + + "handlers": { + "console": { + "level": "DEBUG", + #"class": "logging.StreamHandler", + "class": "rq.utils.ColorizingStreamHandler", + "formatter": "console", + "exclude": ["%(asctime)s"], + }, + }, + + "root": { + "handlers": ["console"], + "level": "DEBUG" if verbose else "INFO" + } + }) diff --git a/rq/scripts/rqworker.py b/rq/scripts/rqworker.py index b088dde..6ffe4e5 100755 --- a/rq/scripts/rqworker.py +++ b/rq/scripts/rqworker.py @@ -5,6 +5,7 @@ import logging import logging.config from rq import Queue, Worker +from rq.logutils import setup_loghandlers from redis.exceptions import ConnectionError from rq.scripts import add_standard_arguments from rq.scripts import setup_redis @@ -14,36 +15,6 @@ from rq.scripts import setup_default_arguments logger = logging.getLogger(__name__) -def setup_loghandlers(args): - if not logging._handlers: - logging.config.dictConfig({ - "version": 1, - "disable_existing_loggers": False, - - "formatters": { - "console": { - "format": "%(asctime)s %(message)s", - "datefmt": "%H:%M:%S", - }, - }, - - "handlers": { - "console": { - "level": "DEBUG", - #"class": "logging.StreamHandler", - "class": "rq.utils.ColorizingStreamHandler", - "formatter": "console", - "exclude": ["%(asctime)s"], - }, - }, - - "root": { - "handlers": ["console"], - "level": "DEBUG" if args.verbose else "INFO" - } - }) - - def parse_args(): parser = argparse.ArgumentParser(description='Starts an RQ worker.') add_standard_arguments(parser) @@ -74,7 +45,7 @@ def main(): if args.sentry_dsn is None: args.sentry_dsn = settings.get('SENTRY_DSN', None) - setup_loghandlers(args) + setup_loghandlers(args.verbose) setup_redis(args) try: diff --git a/rq/worker.py b/rq/worker.py index 4d5c0b4..c2133ea 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -17,6 +17,7 @@ from .queue import Queue, get_failed_queue from .connections import get_current_connection from .job import Status from .utils import make_colorizer +from .logutils import setup_loghandlers from .exceptions import NoQueueError, UnpickleError from .timeouts import death_penalty_after from .version import VERSION @@ -280,6 +281,7 @@ class Worker(object): The return value indicates whether any jobs were processed. """ + setup_loghandlers() self._install_signal_handlers() did_perform_work = False