如何在django站点上记录服务器错误 [英] How do you log server errors on django sites
问题描述
所以,当玩这个开发时,我可以设置 settings.DEBUG
到 True
,如果发生错误我可以看到它很好地格式化,具有良好的堆栈跟踪和请求信息。
So, when playing with the development I can just set settings.DEBUG
to True
and if an error occures I can see it nicely formatted, with good stack trace and request information.
但是在生产站点上,我宁愿使用 DEBUG = False
,并向访客显示一些标准错误500页,其中包含我正在修复此错误的信息;)
同时我想要一些方式将所有这些信息(堆栈跟踪和请求信息)记录到我的服务器上的文件中 - 所以我可以将其输出到我的控制台并观看错误滚动,每小时将电子邮件发送给我或类似的东西。
But on kind of production site I'd rather use DEBUG=False
and show visitors some standard error 500 page with information that I'm working on fixing this bug at this moment ;)
At the same time I'd like to have some way of logging all those information (stack trace and request info) to a file on my server - so I can just output it to my console and watch errors scroll, email the log to me every hour or something like this.
您将建议使用什么样的日志解决方案进行django站点,这将满足这些简单的要求?我有应用程序作为 fcgi
服务器运行,我使用apache web服务器作为前端(虽然想到lighttpd)。
What logging solutions would you recomend for a django-site, that would meet those simple requirements? I have the application running as fcgi
server and I'm using apache web server as frontend (although thinking of going to lighttpd).
推荐答案
当 DEBUG = False
时,Django会自动向每个列出的人员发送任何错误的完整追溯 ADMINS
设置,这可以让您免费获得通知。如果你想要更精细的控制,你可以编写和添加一个中间件类,它定义了一个名为 process_exception()
的方法,它将访问例外:
Well, when DEBUG = False
, Django will automatically mail a full traceback of any error to each person listed in the ADMINS
setting, which gets you notifications pretty much for free. If you'd like more fine-grained control, you can write and add to your settings a middleware class which defines a method named process_exception()
, which will have access to the exception that was raised:
http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-exception
您的 process_exception()
方法可以执行任何类型的日志记录:写入控制台,写入文件等。
Your process_exception()
method can then perform whatever type of logging you'd like: writing to console, writing to a file, etc., etc.
编辑:虽然这不太有用,但您也可以收听 got_request_exception
信号,当请求中遇到异常时将发送处理:
though it's a bit less useful, you can also listen for the got_request_exception
signal, which will be sent whenever an exception is encountered during request processing:
http://docs.djangoproject.com/en/dev/ref/signals/#got-request-exception
这样做不会让您访问异常对象,所以中间件方法更容易使用。
This does not give you access to the exception object, however, so the middleware method is much easier to work with.
这篇关于如何在django站点上记录服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!