Django 1.3日志记录:不记录500个错误

编程入门 行业动态 更新时间:2024-10-26 00:27:17
本文介绍了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个错误

本文发布于:2023-11-07 14:51:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566747.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   日志   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!