django-q icon indicating copy to clipboard operation
django-q copied to clipboard

Logging configuration ignored

Open quantoid opened this issue 6 years ago • 2 comments

Django logging configuration (in settings module) is ignored because django_q.conf overrides it:

# logger
logger = logging.getLogger('django-q')

# Set up standard logging handler in case there is none
if not logger.handlers:
    logger.setLevel(level=getattr(logging, Conf.LOG_LEVEL))
    logger.propagate = False
    formatter = logging.Formatter(fmt='%(asctime)s [Q] %(levelname)s %(message)s',
                                  datefmt='%H:%M:%S')
    handler = logging.StreamHandler()
    handler.setFormatter(formatter)
    logger.addHandler(handler)

In most cases our settings will add handlers to the root logger and not explicitly add handlers to the django-q logger, so the above code will always create its own handler and prevent propagation to the root logger. This means you see logging in the format dictated by Django-Q rather than the format configured in your settings.

There's no easy way to override this so that logging is simply propagated to the root logger and handled there, like all other logging in Django.

My preference would be for Django-Q to only configure its own logger if that's specified in the configuration options. Alternatively add an option to prevent this logging override behaviour.

quantoid avatar May 28 '19 01:05 quantoid

You basically have to configure a handler that discards logging and use that to configure the django-q logger, which seems clunky and non-intuitive.

    'handlers': {
        # Discard logging (for when a handler is mandatory).
        'discard': {
            'class': 'logging.NullHandler',
        },
        # ....
    },
    'loggers': {
        'django-q': {
            'level': 'INFO',
            'propagate': True,
            'handlers': ['discard'],
        },
        # ....
    }

quantoid avatar May 28 '19 01:05 quantoid

Though I've been able to force it to json format the event text so our log processing doesn't break, the fact django-q's logging config doesn't respect normal python logging configuration is quite frustrating. It makes filtering and categorising log messages much harder than it needs to be. I don't really want to fork the whole project over something as rivial as log setup. Does anyone have any pull requests from their own forks that could be proposed to fix this?

techdragon avatar Mar 08 '22 07:03 techdragon