【问题标题】:Logging 404 Not Found pages to Sentry with Django使用 Django 将 404 Not Found 页面记录到 Sentry
【发布时间】:2015-03-25 10:13:08
【问题描述】:

我想在 Django 1.7 中将 404 Not Found 错误代码记录到 Sentry。

Django 提供了哪些钩子,以便我可以为这些推送logger.error() 消息?

任何其他想法应该如何使用 Django 和 Sentry 监控非 500 个丢失/行为异常的页面?

【问题讨论】:

    标签: django sentry raven


    【解决方案1】:

    查看 Sentry Docs for Django 的 Reporting other status codes 部分:

    如果您想向 Sentry 报告 404 Not Found 和除未捕获异常(500 内部服务器错误)之外的其他错误,您需要为这些状态代码编写自己的 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")
    

    有关自定义错误的更多信息,请阅读relevant Django Docs

    【讨论】:

      【解决方案2】:

      Sentry 有一个中间件可以自动记录 404。

      # Use ``MIDDLEWARE_CLASSES`` prior to Django 1.10
      MIDDLEWARE = (
          'raven.contrib.django.raven_compat.middleware.Sentry404CatchMiddleware',
          ...,
      ) + MIDDLEWARE
      

      请参阅 Sentry Django 文档中的 "404 Logging" 了解更多信息,包括如何忽略某些路径的 404。

      【讨论】:

      【解决方案3】:

      查看自定义错误处理程序的文档。

      基本上,我认为您可能希望为要捕获的错误设置视图,这些视图可以呈现您需要的模板或记录消息等; Customising error views。 显然,在这些视图中,您可以访问请求对象的部分内容以收集您可能需要的任何信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-03
        • 2014-01-26
        • 2021-03-07
        • 2021-07-25
        • 1970-01-01
        • 2018-04-18
        • 2014-02-16
        相关资源
        最近更新 更多