Django 1.3日志记录:不记录500个错误 [英] Django 1.3 logging: 500 errors are not logged

查看:147
本文介绍了Django 1.3日志记录:不记录500个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使'django.request'记录器按照广告的方式工作 - 500个错误似乎不会传播到附加的处理程序。



每个新项目的默认日志记录配置,评论说通过此配置执行的日志记录是在每个HTTP 500错误上向站点管理员发送电子邮件。显然,只有在ADMINS设置正确的情况下才会发送电子邮件,但是当视图引发异常时,我甚至不会看到处理程序被调用。



testcase从一个空项目开始,并将我自己的处理程序添加到'django.request'记录器中:

  LOGGING = {
'version':1,
'disable_existing_loggers':False,
'handlers':{
'mail_admins':{
'level':'ERROR',
'class':'django.utils.log.AdminEmailHandler'
},
'my_error_handler':{
'level':'ERROR',
'class' 'log.MyErrorHandler'
}
},
'loggers':{
'django.request':{
'handlers':['mail_admins',' my_error_handler'],
'level':'ERROR',
'propagate':True,
},
}
}

处理程序本身:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s %record

如果我自己在记录器上调用.error(),一切都可以正常工作。但是当一个视图生成一个500时,这两个处理程序都不会被调用。我的views.py:

  import logging 

def home(request):

#this错误将由MyErrorHandler
logging.getLogger('django.request')处理错误(自定义错误消息)

#this不会
raise异常('500错误消息')

返回HttpResponse(Home)

其他都是默认的项目; DEBUG为True,中间件配置不变。有没有一些隐藏的配置选项,我需要启用这样的工作,如在文档中所述?



谢谢,
Matt。

解决方案

使用 DEBUG 设置为 False 。启用调试时,您的 settings.py 中的错误处理程序将被覆盖。


I'm struggling to make the 'django.request' logger work as advertised - 500 errors don't seem to propagate to handlers attached to it.

In the default logging configuration for every new project, the comment says the "logging performed by this configuration is to send an email to the site admins on every HTTP 500 error". Obviously the email is only sent if you've setup ADMINS correctly, but I'm not even seeing the handler get called when a view raises an exception.

I built a testcase starting with an empty project, and adding my own handler to the 'django.request' logger:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'my_error_handler' : {
            'level': 'ERROR',
            'class': 'log.MyErrorHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins', 'my_error_handler'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

The handler itself:

import logging
class MyErrorHandler(logging.Handler):     
    def emit(self, record):
        print "handling error: %s" % record 

If I call .error() on the logger myself, everything works as it should. But when a 500 is generated by a view, neither of the handlers are called. My views.py:

import logging

def home(request):

    #this error will be processed by MyErrorHandler
    logging.getLogger('django.request').error("Custom error message")

    #this won't
    raise Exception('500 error message')

    return HttpResponse("Home")

Everything else is the project is default; DEBUG is True, the middleware config is unchanged. Is there some hidden config option I need to enable to have this work as it says in the docs?

Thanks, Matt.

解决方案

Try it with DEBUG set to False. When debugging is enabled, the error handlers in your settings.py get overridden.

这篇关于Django 1.3日志记录:不记录500个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆