Koed00 / django-q

A multiprocessing distributed task queue for Django
https://django-q.readthedocs.org
MIT License
1.83k stars 285 forks source link

Use logger.hasHandlers() to setup fallback logging #672

Open wesleyjellis opened 2 years ago

wesleyjellis commented 2 years ago

Instead of checking if the django-q logger has any handlers directly, use the hasHandlers method, which will check if any parent loggers have handlers as well.

This fixes a bug where django-q configures it's own logging even though the root logger has a handler set

import logging

root_logger = logging.getLogger()
django_q_logger = logging.getLogger("django-q")

print("list of djando_q handlers", django_q_logger.handlers)
# []
print("does django_q have handlers?", django_q_logger.hasHandlers())
# False

root_logger.addHandler(logging.StreamHandler())

print("list of djando_q handlers", django_q_logger.handlers)
# []
print("does django_q have handlers?", django_q_logger.hasHandlers())
# True