【问题标题】:How to send Exceptions to sentry for a django project如何将异常发送到 Django 项目的哨兵
【发布时间】:2019-02-16 16:54:24
【问题描述】:

按照Sentry Django guide,我已经设置并部署了我的项目。

所以我用SENTRY_DSN 值和release 值配置RAVEN_CONFIG,并在我的INSTALLED_APPS 中包含raven.contrib.django.raven_compat

我已确认使用此配置,以下命令将在哨兵中正确生成一条消息。

python manage.py raven test

但是,我创建了以下 django 视图来引发异常,作为确认哨兵正在工作的另一种方法,当我点击此视图时,我得到了 500 响应,但哨兵中没有任何显示。

app1/views.py

def error(request):
    x = 1/0  # error for sentry testing

我的期望是 any 异常(包括 500 个错误) 发生在 django 会被发送到哨兵而不需要使用 记录。

我对渡鸦哨兵配置的理解有误吗? 或者,我需要配置其他东西吗?

我正在使用 django 的 LOGGING 设置,但目前我不在乎这些消息是否“错误”或以其他方式发送到哨兵。我目前的主要目标是捕获并将发生的任何异常发送给哨兵。

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'format': '{asctime} [{levelname:5}] ({name}) {funcName}: {message}',
            'style': '{',
        }
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'standard',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': DJANGO_CORE_LOG_LEVEL,  # Change to DEBUG to see db queries
        },
        'app1': {
            'handlers': ['console'],
            'level': DJANGO_LOG_LEVEL,
            'propagate': True,
        },
        'app2': {
            'handlers': ['console'],
            'level': DJANGO_LOG_LEVEL,
            'propagate': True,
        }
    },
}

更新

我已经设法让app1.views.error 视图在本地使用 runserver 时向哨兵报告错误,但是在更新配置和部署后,它在部署时仍然无法正常工作。 (manage raven test 确实如此)

【问题讨论】:

  • 请发布您的LOGGING设置值

标签: python django sentry raven


【解决方案1】:

来自哨兵文档

LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'root': {
    'level': 'WARNING',
    'handlers': ['sentry'],
},
'formatters': {
    'verbose': {
        'format': '%(levelname)s  %(asctime)s  %(module)s '
                  '%(process)d  %(thread)d  %(message)s'
    },
},
'handlers': {
    'sentry': {
        'level': 'ERROR', # To capture more than ERROR, change to WARNING, INFO, etc.
        'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
        'tags': {'custom-tag': 'x'},
    },
    'console': {
        'level': 'DEBUG',
        'class': 'logging.StreamHandler',
        'formatter': 'verbose'
    }
},
'loggers': {
    'django.db.backends': {
        'level': 'ERROR',
        'handlers': ['console'],
        'propagate': False,
    },
    'raven': {
        'level': 'DEBUG',
        'handlers': ['console'],
        'propagate': False,
    },
    'sentry.errors': {
        'level': 'DEBUG',
        'handlers': ['console'],
        'propagate': False,
    },
},

}

文档链接:https://docs.sentry.io/clients/python/integrations/django/

编辑:来自评论

try:
    do something
except Exception:
    from raven.contrib.django.raven_compat.models import client
    client.captureException()

如果您不想手动执行此操作,并且希望在项目中任何地方触发异常块时随时进行哨兵日志记录,请使用上述基于日志记录的解决方案。

【讨论】:

  • 我确实看到了这个,但是如果这是必需,文档似乎并不清楚。它提到 To integrate with the standard library’s logging module, and send all ERROR and above messages to sentry ,但我不关心日志集成,是否发送 logging.error() 消息并不重要。我只希望异常传播到哨兵。
  • 文档没有提到它是否需要,因为它不是。您也可以从触发异常的地方传播它。我正在编辑答案以适应这一点。
  • 谢谢,在之前的测试中,我相信我设法在没有手动设置client.captureException() 的情况下将异常发送到哨兵。 似乎应该可以设置并让所有异常传播到哨兵,而无需将client.captureException()导入并添加到django项目中的每个文件......但是因为它不起作用,所以我不明白,或者配置不正确......
  • 我正在做类似这里描述的事情:scotch.io/tutorials/integrating-sentry-into-django-applications
  • 好的,经过一些测试后,似乎如果您定义了 LOGGING 配置,则此额外的 LOGGING 设置是必需的。我猜如果没有'root' 配置,django 记录器会窃取异常,因此哨兵/乌鸦无法到达它...
【解决方案2】:

正如@Mehran 在settings.py 中配置您的LOGGING 后所说,您将能够使用日志记录。假设您在loggers 中有example,配置如下:

'loggers': {        
    'example': {
        'handlers': ['console', 'sentry'],
        'level': 'DEBUG',
        'propagate': False
    }
}

那么;

import logging

logger = logging.getLogger("example")

def test():
    try:
       # some staff
    except Exception as error:
       logger.error("Custom Error Message %s" %error)

logger 也有warninfo 等。

【讨论】:

  • 是的,我知道这使您可以使用日志记录模块向哨兵发送消息,但这不是我想要的。我希望发送未处理的异常。
【解决方案3】:

也可以直接发布到您的 Sentry DSN 而不使用任何 Sentry 库,但使用纯 requests。如果您运行的 Python 或 Django 版本与 Sentry 的最新 DSN 不兼容,这会很有用。

我已经为此发布了一个代码 - 包括一个示例测试 - here

【讨论】:

    【解决方案4】:

    只是检查说其他答案完全过时了。

    raven 库已弃用,取而代之的是 sentry-sdk,并且设置它不再涉及修改日志记录配置,因为默认的日志记录集成已经监视任何记录器调用。

    做事

    import sentry_sdk.integrations.django
    
    sentry_sdk.init(
        integrations=[sentry_sdk.integrations.django.DjangoIntegration()],
    )
    

    在您的 settings.py 中,一切顺利(记得导出 SENTRY_DSN 变量等等)。

    https://docs.sentry.io/platforms/python/guides/django/#configure

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      • 2017-10-25
      • 2015-10-04
      相关资源
      最近更新 更多