0

I'd like to use logging.config.dictConfig within my RQ worker. But, I found that after I run dictConfig(), the worker will no longer raise exceptions which won't work in production.

Here's my worker code:

import logging
import logging.config

config = {
    'version': 1,
    'disable_existing_loggers': True,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'stream': 'ext://sys.stdout',
            'level': 'DEBUG'
        }
    },
    'root': {
        'level': 'DEBUG',
        'handlers': ['console']
    }
}

logging.config.dictConfig(config)

log = logging.getLogger('test')


def raises():
    '''This is the worker function'''
    log.info('running raises')
    raise RuntimeError

When the worker receives a job, it simply halts execution after the log statement, with no exception being raised:

12:28:09 log-test: log_test.raises() (d59ad742-4dcd-4d4f-84e2-6f747c21d603)
running raises

EDIT: Another interesting piece of the puzzle is that sys.excepthook is not called in the worker context.

matthewatabet
  • 1,463
  • 11
  • 26

1 Answers1

0

It turns out that RQ is quite tightly coupled to its logging setup. In fact, the Worker class holds an instance of a Logger -- when dictConfig is called, the references to this logger break.

The solution was to include an rq.worker logger in my config:

config = {
    'version': 1,
    'formatters': {
        'rq': {
            'format': '%(asctime)s %(message)s',
            'datefmt': '%H:%M:%S'
        }
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'stream': 'ext://sys.stdout',
            'level': 'DEBUG'
        },
        'rq': {
            'class': 'rq.utils.ColorizingStreamHandler',
            'formatter': 'rq'
        }
    },
    'loggers': {
        'rq.worker': {
            'level': 'INFO',
            'handlers': ['rq'],
            'propagate': False
        }
    },
    'root': {
        'class': 'logging.RootLogger',
        'level': 'DEBUG',
        'handlers': ['console']
    }
}
matthewatabet
  • 1,463
  • 11
  • 26