Merge pull request #642 from butla/master

Worker only creates log handlers if there aren't any.
main
Selwin Ong 9 years ago
commit af0b19e707

@ -16,10 +16,25 @@ from rq.utils import ColorizingStreamHandler
def setup_loghandlers(level='INFO'): def setup_loghandlers(level='INFO'):
logger = logging.getLogger('rq.worker') logger = logging.getLogger('rq.worker')
if not logger.handlers: if not _has_effective_handler(logger):
logger.setLevel(level) logger.setLevel(level)
formatter = logging.Formatter(fmt='%(asctime)s %(message)s', formatter = logging.Formatter(fmt='%(asctime)s %(message)s',
datefmt='%H:%M:%S') datefmt='%H:%M:%S')
handler = ColorizingStreamHandler() handler = ColorizingStreamHandler()
handler.setFormatter(formatter) handler.setFormatter(formatter)
logger.addHandler(handler) logger.addHandler(handler)
def _has_effective_handler(logger):
"""
Checks if a logger has a handler that will catch its messages in its logger hierarchy.
:param `logging.Logger` logger: The logger to be checked.
:return: True if a handler is found for the logger, False otherwise.
:rtype: bool
"""
while True:
if logger.handlers:
return True
if not logger.parent:
return False
logger = logger.parent

Loading…
Cancel
Save