Getting rid of Django IOErrors

后端 未结 3 1869
星月不相逢
星月不相逢 2021-01-02 19:54

I\'m running a Django site (via Apache/mod_python) and I use Django\'s facilities to inform me and other developers about internal server errors. Sometimes errors like those

3条回答
  •  借酒劲吻你
    2021-01-02 20:36

    Extending the solution by @dlowe for Django 1.3, we can write the full working example as:

    settings.py

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'filters': {
            'supress_unreadable_post': {
                '()': 'common.logging.SuppressUnreadablePost',
            }
        },
        'handlers': {
            'mail_admins': {
                'level': 'ERROR',
                'class': 'django.utils.log.AdminEmailHandler',
                'filters': ['supress_unreadable_post'],
            }
        },
        'loggers': {
            'django.request': {
                'handlers': ['mail_admins'],
                'level': 'ERROR',
                'propagate': True,
            },
        }
    }
    

    common/logging.py

    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
    

提交回复
热议问题