django - 使用 Django 将 404 Not Found 页面记录到 Sentry

标签 django sentry raven

我想将 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/

相关文章:

python - 两个 Pylons 记录器处理程序(Sentry/Raven 和控制台)用于相同的 qualname

django - 当 DEBUG = True 时,为什么断言会记录在 Sentry 中?

python - 在 nginx ubuntu 下服务多个 gunicorn django 实例

django - 相同的字段,Django模型子类中的不同选择

javascript - 生产中的源映射文件——安全吗?

android - Sentry 跟踪移动设备操作系统,设备 - 应该如何指定 header 才能使其正常工作?

python - py.test : Show local variables in Jenkins

python - 如何通过单独模型中最新对应条目中的文本字段过滤 Django 查询集

html - django + html 渲染

javascript - 在使用 grunt usemin 和 rev 时使用 SourceMaps Uglify