我想将 404 Not Found 错误代码记录到 Django 1.7 中的 Sentry。
Django 提供了哪些钩子(Hook)以便我可以推送这些消息的 logger.error()
消息?
还有其他想法如何使用 Django 和 Sentry 监控非 500 个缺失/行为异常的页面吗?
最佳答案
查看 Reporting other status codes Django 的 Sentry 文档部分:
如果你想向 Sentry 报告 404 Not Found 以及除了未捕获的异常(500 Internal Server Error)之外的其他错误,你需要为这些状态代码编写自己的 Django View 。例如:
# 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")
有关自定义错误的更多信息,请阅读 relevant Django Docs
关于django - 使用 Django 将 404 Not Found 页面记录到 Sentry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28159604/