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.
main
Vincent Driessen 12 years ago
parent 40d0a7d9a9
commit 37b3bb4bd4

@ -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"
}
})

@ -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:

@ -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

Loading…
Cancel
Save