0

In my Django Project, I get informed about IOErrors that happen sometimes when users upload data to the website. I wan't to turn off the email notification when IOErrors happen.

I found out how to write a filter, so I can get rid of the IOError. But how can I change this to log the error, but only to console, not per mail.

settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        },
        'suppress_unreadable_post': {
            '()': 'django.utils.log.CallbackFilter',
            'callback': SuppressUnreadablePost,
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'include_html': True,
            'class': 'django.utils.log.AdminEmailHandler'
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'myapp': {
            'handlers': ['console'],
            'level': 'DEBUG',
        },
        'ratelimitbackend': {
            'handlers': ['console'],
            'level': 'DEBUG',
        },
    }
}

And my filter:

import sys, traceback

class SuppressUnreadablePost(object):
    def filter(self, record):
        _, exception, tb = sys.exc_info()
        if isinstance(exception, IOError):
            for _, _, function, _ in traceback.extract_tb(tb):
                if function == '_get_raw_post_data':
                    return False
        return True

Ideas :)? Thank you!

Community
  • 1
  • 1
TheWaveLad
  • 966
  • 2
  • 14
  • 39
  • possible duplicate of [Getting rid of Django IOErrors](http://stackoverflow.com/questions/2375950/getting-rid-of-django-ioerrors) – Thom Wiggers Mar 03 '15 at 15:21

1 Answers1

2

Assuming your filter is correct, you only need to edit your mail_admins handler to add the filter:

   'mail_admins': {
        'level': 'ERROR',
        'filters': ['require_debug_false', 'suppress_unreadable_post'],
        'include_html': True,
        'class': 'django.utils.log.AdminEmailHandler'
    }

Like https://docs.djangoproject.com/en/1.7/topics/logging/#django.utils.log.CallbackFilter

Thom Wiggers
  • 6,938
  • 1
  • 39
  • 65