Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my Django 1.3 logging setup cause all messages to be output twice?

Tags:

logging

django

My Django 1.3 logging setup causes all log messages to be output twice. I've read that importing settings.py two or more times has caused this problem in the past, but Django 1.3 has new logging features, and I don't think I'm importing settings.py twice anywhere.

settings.py config:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'simple': {
            'format': '%(levelname)s: %(message)s'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple',
        },
    },
    'loggers': {
        'custom': {
            'handlers': ['console'],
            'level': 'DEBUG',
        },
    }
}

Code:

import logging
log = logging.getLogger('custom')

log.debug('message1')
log.debug('message2')

Output:

DEBUG: message1
DEBUG:custom:message1

Thanks for your help.

like image 522
mpso Avatar asked Jul 17 '11 07:07

mpso


1 Answers

Have you tried setting propagate = False? Along with disable_existing_loggers = True?

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'simple': {
            'format': '%(levelname)s: %(message)s'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple',
        },
    },
    'loggers': {
        'custom': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        },
    }
}
like image 91
Ken Cochrane Avatar answered Nov 16 '22 02:11

Ken Cochrane