如何在 django 站点上记录服务器错误
因此,在进行开发时,我只需将 settings.DEBUG
设置为 True
,如果发生错误,我可以看到它的格式很好,具有良好的堆栈跟踪和请求信息。
但在某种生产站点上,我宁愿使用 DEBUG=False
并向访问者显示一些标准错误 500 页面,其中包含我目前正在修复此错误的信息;)
同时,我希望有某种方法将所有这些信息(堆栈跟踪和请求信息)记录到我的服务器上的文件中 - 这样我就可以将其输出到我的控制台并观察错误滚动,将日志通过电子邮件发送给我每小时或类似的时间。
您会为 django 站点推荐哪些日志记录解决方案来满足这些简单的要求? 我将应用程序作为 fcgi 服务器运行,并使用 apache Web 服务器作为前端(尽管考虑使用 lighttpd)。
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.
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.
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
好吧,当 DEBUG = False 时,Django 会自动将任何错误的完整回溯邮件发送给 ADMINS 设置中列出的每个人,这几乎免费为您提供通知。 如果您想要更细粒度的控制,您可以编写一个中间件类并将其添加到您的设置中,该类定义了一个名为
process_exception()
的方法,该方法将有权访问引发的异常:< a href="http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-exception" rel="noreferrer">http://docs.djangoproject.com/en/dev/topics /http/middleware/#process-exception
然后,您的
process_exception()
方法可以执行您想要的任何类型的日志记录:写入控制台、写入文件等 编辑:就会发送该信号:
虽然它有点不太有用,但您也可以监听
got_request_exception
信号,只要在请求处理过程中遇到异常, djangoproject.com/en/dev/ref/signals/#got-request-exception" rel="noreferrer">http://docs.djangoproject.com/en/dev/ref/signals/#got-request-exception< /a>但是,这不能让您访问异常对象,因此中间件方法更容易使用。
Well, when
DEBUG = False
, Django will automatically mail a full traceback of any error to each person listed in theADMINS
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 namedprocess_exception()
, which will have access to the exception that was raised:http://docs.djangoproject.com/en/dev/topics/http/middleware/#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.Edit: 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 Sentry 是一个很好的方法,但是正确设置它(作为一个单独的网站)需要一些工作。 如果您只想将所有内容记录到一个简单的文本文件中,那么这里是要放入
settings.py
中的日志记录配置Django Sentry is a good way to go, as already mentioned, but there is a bit of work involved in setting it up properly (as a separate website). If you just want to log everything to a simple text file here's the logging configuration to put in your
settings.py
另一个答案中提到的 django-db-log 已替换为:
https://github.com/dcramer /django-sentry
django-db-log, mentioned in another answer, has been replaced with:
https://github.com/dcramer/django-sentry
显然 James 是正确的,但如果您想在数据存储中记录异常,已经有一些开源解决方案可用:
1) CrashLog 是一个不错的选择:http://code.google.com/p/django-crashlog/
2) Db-Log 也是一个不错的选择:http://code.google.com/p/django-db-log/
什么是两者有何区别? 我几乎看不到任何东西,所以任何一个就足够了。
我都用过,效果都很好。
Obviously James is correct, but if you wanted to log exceptions in a datastore, there are a few open source solutions already available:
1) CrashLog is a good choice: http://code.google.com/p/django-crashlog/
2) Db-Log is a good choice as well: http://code.google.com/p/django-db-log/
What is the difference between the two? Almost nothing that I can see, so either one will suffice.
I've used both and they work well.
自 EMP 提交最有帮助的代码以来已经过去了一段时间了。 我刚刚实现了它,在尝试使用一些manage.py选项来尝试追查错误时,我收到了一个弃用警告,大意是在我当前版本的Django(1.5.?)中,现在有一个require_debug_false过滤器mail_admins 处理程序需要。
这是修改后的代码:
Some time has passed since EMP's most helpful code submission. I just now implemented it, and while thrashing around with some manage.py option, to try to chase down a bug, I got a deprecation warning to the effect that with my current version of Django (1.5.?) a require_debug_false filter is now needed for the mail_admins handler.
Here is the revised code:
我的
fcgi
脚本刚刚遇到了一个烦人的问题。 它发生在 django 启动之前。 缺乏日志记录真是太痛苦了。 无论如何,将 stderr 重定向到文件作为第一件事有很大帮助:I just had an annoying problem with my
fcgi
script. It occurred before django even started. The lack of logging is sooo painful. Anyway, redirecting stderr to a file as the very first thing helped a lot:您可以在 Python 中使用日志记录库,无需
pip install
任何内容。将任何
print()
替换为logging.debug()
但,
正如 EMP 所说,
You can use the logging library in Python, no need to
pip install
anything.Replace any
print()
withlogging.debug()
but,
as EMP said.