我想在 Django 1.7 中将404 Not Found错误代码记录到 Sentry。
Django 提供了哪些钩子,以便我可以为这些推送logger.error()
消息?
任何其他想法应该如何使用 Django 和 Sentry 监控非 500 个丢失/行为异常的页面?
查看 Sentry Docs for Django的报告其他状态代码部分:
如果您想向 Sentry 报告 404 Not Found 和除未捕获异常(500 Internal Server Error)之外的其他错误,您需要为这些状态代码编写自己的 Django 视图。例如:
# in the root URL conf urls.py
handler404 = 'myapp.views.my_custom_page_not_found_view'
# myapp/views.py
from django.http import HttpResponseNotFound
from sentry_sdk import capture_message
def my_custom_page_not_found_view(*args, **kwargs):
capture_message("Page not found!", level="error")
# return any response here, e.g.:
return HttpResponseNotFound("Not found")
有关自定义错误的更多信息,请阅读相关的 Django 文档
Sentry 有一个中间件可以自动记录 404。
# Use ``MIDDLEWARE_CLASSES`` prior to Django 1.10
MIDDLEWARE = (
'raven.contrib.django.raven_compat.middleware.Sentry404CatchMiddleware',
...,
) + MIDDLEWARE
有关更多信息,包括如何忽略某些路径的 404,请参阅Sentry Django 文档中的“404 日志记录” 。
Take a look at the docs for custom error handlers.
Basically I think you'll probably want to setup views for the errors you want to capture which can render templates you need or log messages etc; Customising error views. Obviously in those views you can access parts of the request object to collect any information you might need.