Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 655
apelliciari Avatar asked May 27 '13 16:05

apelliciari


2 Answers

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.

like image 146
John Lehmann Avatar answered Sep 19 '22 01:09

John Lehmann


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

like image 34
karthikr Avatar answered Sep 22 '22 01:09

karthikr