Formatting Django Management Command Logging

Viewed 399

I have this django setting:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'DEBUG',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'DEBUG',
            'propagate': True,
        },
    }
}

In my management command code, I had to put this to log at debug level:

import logging
logger = logging.getLogger('main')
logger.setLevel(logging.DEBUG)

but I want to also add the date so I tried:

import logging
logger = logging.getLogger('main')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

and this results in things getting logged twice like:

2016-03-14 22:32:16,202 DEBUG: test
DEBUG: test

How can format the logger for the management command?

0 Answers
Related