apelliciari
apelliciari

Reputation: 8501

Django: how to log exceptions from management commands?

I don't receive mails from errors happened in commands.

python deebate\manage.py test_logging_errors --settings=deebate.settings.local --traceback

the command:

# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand, CommandError

import logging
logger = logging.getLogger(__name__)

    class Command(BaseCommand):
        help = "test unicode and logging"

        def handle(self, *args, **options):
            print(u"|`»|Ð".encode('ascii'))

Obviously this throws

UnicodeEncodeError: 'ascii' codec can't encode character u'\xbb' in position 2: ordinal not in range(128)

I have DEBUG = False

LOGGING setting is

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple',
        },
       # I always add this handler to facilitate separating loggings
        'debug_log_file':{
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(ROOT_PROJECT_INTERNAL, 'logs', 'debug.log'),
            'maxBytes': '16777216', # 16megabytes
            'backupCount': 10,
            'formatter': 'verbose'
        },
        'warning_log_file':{
            'level': 'WARNING',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(ROOT_PROJECT_INTERNAL, 'logs', 'warning.log'),
            'maxBytes': '16777216', # 16megabytes
            'backupCount': 10,
            'formatter': 'verbose'
        },
        'django_log_file':{
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(ROOT_PROJECT_INTERNAL, 'logs', 'django.log'),
            'maxBytes': '16777216', # 16megabytes
            'backupCount': 10,
            'formatter': 'verbose'
        },

    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins', 'django_log_file'],
            'level': 'ERROR',
            'propagate': True,
        },
        'core': {
            'handlers': ['mail_admins', 'debug_log_file', 'warning_log_file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    }
}

and then i have Sentry too.

debug.log and warning.log are regularly filled by the app.

How do i catch that exception? Why Django doesn't catch that?

Upvotes: 8

Views: 4681

Answers (2)

John Lehmann
John Lehmann

Reputation: 8225

I appreciated this article which explains how to setup your AdminEmailHandler, but also to modify your manage.py to catch exceptions and log them, so that ALL management commands will send the email.

Upvotes: 14

karthikr
karthikr

Reputation: 99680

You could set the AdminEmailHandler to get an email for error from management commands

Upvotes: 2

Related Questions