How do you log server errors on django sites
因此,在使用开发时,我可以将
但是在生产网站上,我宁愿使用
同时我想有办法将所有这些信息(堆栈跟踪和请求信息)记录到我服务器上的文件中 - 所以我可以将它输出到我的控制台并观察错误滚动,将日志通过电子邮件发送给我 每小时或类似的事情。
您会为django站点推荐哪些日志记录解决方案,以满足这些简单的要求? 我将应用程序作为
好吧,当
http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-exception
然后,您的
编辑:虽然它没那么有用,但您也可以监听
http://docs.djangoproject.com/en/dev/ref/signals/#got-request-exception
但是,这并不能让您访问异常对象,因此中间件方法更容易使用。
正如前面提到的,Django Sentry是一个很好的方法,但是正确设置它需要做一些工作(作为一个单独的网站)。如果您只想将所有内容记录到一个简单的文本文件中,那么就是要放入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 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 }, }, } |
另一个答案中提到的django-db-log已被替换为:
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/
两者有什么区别?几乎没有什么我能看到的,所以任何一个都足够了。
我已经使用过它们并且效果很好。
自EMP最有用的代码提交以来已经过去了一段时间。我刚刚实现了它,并且在使用一些manage.py选项时,为了试图追查一个bug,我得到了一个弃用警告,结果是我当前版本的Django(1.5。?)现在有一个require_debug_false过滤器mail_admins处理程序所需的。
这是修改后的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 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 }, }, } |
我的
1 2 | #!/home/user/env/bin/python sys.stderr = open('/home/user/fcgi_errors', 'a') |