django 1.8无法覆盖AdminEmailHandler

时间:2015-04-18 19:35:01

标签: python django logging django-settings

我正在使用django 1.8并尝试自定义班级 django.utils.log.AdminEmailHandler

这是我的自定义类

class MyCustomAdminEmailHandler(AdminEmailHandler):
    # emit copied and overridden from v1.8 so we can add the request .user into the message
    def emit(self, record):
        current_user = None
        try:
            request = record.request
            subject = '%s (%s IP): %s' % (
                record.levelname,
                ('internal' if request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS
                 else 'EXTERNAL'),
                record.getMessage()
            )
            filter = get_exception_reporter_filter(request)
            request_repr = '\n{}'.format(force_text(filter.get_request_repr(request)))
            current_user = request.user
        except Exception:
            subject = '%s: %s' % (
                record.levelname,
                record.getMessage()
            )
            request = None
            request_repr = "unavailable"
        subject = self.format_subject(subject)

        if record.exc_info:
            exc_info = record.exc_info
        else:
            exc_info = (None, record.getMessage(), None)

        message = "User:%s\n\n%s\n\nRequest repr(): %s" % (current_user, self.format(record), request_repr)
        reporter = ExceptionReporter(request, is_email=True, *exc_info)
        html_message = reporter.get_traceback_html() if self.include_html else None
        self.send_mail(subject, message, fail_silently=True, html_message=html_message)

这是我在settings.py中的日志配置的相关部分

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,    
    'handlers': {
        # can't just override this so customize here
        'mail_admins':{
            'level': 'ERROR',
            'class': 'my_utils.logging_utils.MyCustomAdminEmailHandler',
            'include_html': True,
        },

    },
}

当我对此进行测试时,我没有收到HTML电子邮件,也没有在电子邮件中进行自定义。 有谁知道我错过了什么?

------编辑------

@pynchia的评论:django.utils.log已经为记录器提供了这个相关的日志配置

'loggers': {
    'django': {
        'handlers': ['console'],
    },
    'django.request': {
        'handlers': ['mail_admins'],
        'level': 'ERROR',
        'propagate': False,
    },
    'django.security': {
        'handlers': ['mail_admins'],
        'level': 'ERROR',
        'propagate': False,
    },
    'py.warnings': {
        'handlers': ['console'],
    },
}

-----第二次编辑------ 记录@pynchia的评论有效。我不得不添加手动添加到日志配置。 奇怪的。

0 个答案:

没有答案