harmands
harmands

Reputation: 1112

celery logging in django

I have gone through a lot of questions regarding this on SO, but most of the questions are missing the parameters or implementation methods. So, I'm not able to debug this.

My simple requirement is I want to log all(django and celery) messages to a single file. This is my LOGGING dict in settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'format': '%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)s - %(funcName)s() ] %(name)s: %(message)s'
        },
    },
    'handlers': {
        'default': {
            'level': 'INFO',
            'formatter': 'standard',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'filename': 'logger.log',
            'when': 'midnight',
            'interval': 1
        }
    },
    'loggers': {
        '': {
            'handlers': ['default'],
            'level': 'INFO',
            'propagate': False
        }
    }
}
CELERYD_HIJACK_ROOT_LOGGER = False

Then I have defined this function:

@shared_task
def tester():
    import logging
    logging.info("Log this!!")

tester() is writing to the log file.

tester.delay() is not writing to the log file.

What am I missing in this simple thing?

Upvotes: 2

Views: 2148

Answers (1)

ACimander
ACimander

Reputation: 1979

If you want to define your default log level, use the root key in the LOGGING dict:

...
'root': {
    'handlers': ['default'],
    'level': 'DEBUG'
},
'loggers': {...}
....

In theory, '' as a loggers's key should also work, but I haven't seen it in a while and I know at least in one my projects it didn't work as expected.

See https://docs.python.org/2/library/logging.config.html#dictionary-schema-details

Upvotes: 1

Related Questions