如何记录 django 站点上的服务器错误

因此,当玩开发我可以只设置 settings.DEBUGTrue,如果一个错误发生,我可以看到它很好的格式化,与良好的堆栈跟踪和请求信息。

但在某种生产网站上,我宁愿使用 DEBUG=False,并显示给访问者一些标准错误500页的信息,我正在修复这个错误的时刻;)
与此同时,我希望有一些方法可以将所有这些信息(堆栈跟踪和请求信息)记录到我服务器上的一个文件中——这样我就可以将它输出到我的控制台,观看错误滚动,每小时将日志发送给我,或者类似这样的事情。

对于 django 站点,您推荐哪些日志记录解决方案可以满足这些简单的需求?我的应用程序作为 fcgi服务器运行,我使用 Apache HTTP Server 作为前端(尽管考虑使用 lighttpd)。

99388 次浏览

那么,当 DEBUG = False,Django 将自动邮件任何错误的完整回溯到每个人在 ADMINS设置中列出,这让你的通知几乎是免费的。如果你想要更细粒度的控制,你可以写一个中间件类并添加到你的设置中,它定义了一个名为 process_exception()的方法,这个方法可以访问引发的异常:

Http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-exception

然后,process_exception()方法可以执行任何类型的日志记录: 写到控制台、写到文件等等。

编辑: 虽然它没有那么有用,但是你也可以监听 got_request_exception信号,这个信号会在请求处理过程中遇到异常时发送出去:

Http://docs.djangoproject.com/en/dev/ref/signals/#got-request-exception

但是,没有允许您访问异常对象,因此使用中间件方法要容易得多。

显然 James 是正确的,但是如果您想在数据存储中记录异常,有一些开源解决方案已经可用:

1) CrashLog 是一个不错的选择: http://code.google.com/p/django-crashlog/

2) Db-Log 也是一个不错的选择: http://code.google.com/p/django-db-log/

两者之间有什么区别? 我几乎看不到任何东西,所以任何一个都足够了。

我都用过,效果很好。

另一个答案中提到的 django-db-log 已被替换为:

Https://github.com/dcramer/django-sentry

Django Sentry 是一个很好的方式去,如前所述,但有一点工作涉及到设置正确(作为一个单独的网站)。如果您只想将所有内容记录到一个简单的文本文件中,这里是要放入 settings.py中的日志配置

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
# Include the default Django email handler for errors
# This is what you'd get without configuring logging at all.
'mail_admins': {
'class': 'django.utils.log.AdminEmailHandler',
'level': 'ERROR',
# But the emails are plain text by default - HTML is nicer
'include_html': True,
},
# Log to a text file that can be rotated by logrotate
'logfile': {
'class': 'logging.handlers.WatchedFileHandler',
'filename': '/var/log/django/myapp.log'
},
},
'loggers': {
# Again, default Django configuration to email unhandled exceptions
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
# Might as well log any errors anywhere else in Django
'django': {
'handlers': ['logfile'],
'level': 'ERROR',
'propagate': False,
},
# Your own app - this assumes all your logger names start with "myapp."
'myapp': {
'handlers': ['logfile'],
'level': 'WARNING', # Or maybe INFO or DEBUG
'propagate': False
},
},
}

EMP 最有用的代码提交已经过去了一段时间。我刚刚实现了它,当我使用一些 manage.py 选项来尝试追踪一个 bug 时,我收到了一个反对警告,大意是在我当前版本的 Django (1.5。?)现在,mail _ admins 处理程序需要一个 request _ debug _ false 筛选器。

以下是修订后的守则:

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'handlers': {
# Include the default Django email handler for errors
# This is what you'd get without configuring logging at all.
'mail_admins': {
'class': 'django.utils.log.AdminEmailHandler',
'level': 'ERROR',
'filters': ['require_debug_false'],
# But the emails are plain text by default - HTML is nicer
'include_html': True,
},
# Log to a text file that can be rotated by logrotate
'logfile': {
'class': 'logging.handlers.WatchedFileHandler',
'filename': '/home/username/public_html/djangoprojectname/logfilename.log'
},
},
'loggers': {
# Again, default Django configuration to email unhandled exceptions
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
# Might as well log any errors anywhere else in Django
'django': {
'handlers': ['logfile'],
'level': 'ERROR',
'propagate': False,
},
# Your own app - this assumes all your logger names start with "myapp."
'myapp': {
'handlers': ['logfile'],
'level': 'DEBUG', # Or maybe INFO or WARNING
'propagate': False
},
},
}

我的 fcgi脚本出了点问题。在姜戈开始之前就发生了。伐木的缺乏是如此的痛苦。无论如何,第一件事就是将 stderr 重定向到一个文件:

#!/home/user/env/bin/python
sys.stderr = open('/home/user/fcgi_errors', 'a')

您可以使用 Python 中的日志库,不需要 pip install任何东西。

logging.debug()代替任何 print() 但是,

姜戈哨兵是个不错的选择

正如电磁脉冲所说。