简体   繁体   English

如何向 Django 项目的哨兵发送异常

[英]How to send Exceptions to sentry for a django project

Following the Sentry Django guide I've setup my project and deployed it.按照Sentry Django 指南,我已经设置并部署了我的项目。

So I configure RAVEN_CONFIG with the SENTRY_DSN value and release values, and include raven.contrib.django.raven_compat in my INSTALLED_APPS .所以我用SENTRY_DSN值和release值配置RAVEN_CONFIG ,并在我的INSTALLED_APPS包含raven.contrib.django.raven_compat

I've confirmed that with this configuration the following command will properly generate a message in sentry.我已经确认使用此配置,以下命令将在哨兵中正确生成一条消息。

python manage.py raven test

However, I've created the following django view to raise an exception as an alternative way to confirm that sentry is working, and when I hit this view, I get a 500 response, but nothing shows up in sentry.但是,我创建了以下 django 视图来引发异常作为确认哨兵工作的另一种方法,当我点击这个视图时,我得到了 500 响应,但哨兵中没有任何显示。

app1/views.py app1/views.py

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

My expectation is that any exception (including 500 errors) that occurs in django will be sent to sentry without the need to use logging.我的期望是 django 中发生的任何异常(包括 500 错误)都将被发送到哨兵,而无需使用日志记录。

Is my understanding of the raven sentry configuration wrong?我对乌鸦哨兵配置的理解有误吗? Or, do I need to configure something else?或者,我需要配置其他东西吗?

I am using the LOGGING setting of django, but at the moment I don't care if these messages 'error' or otherwise are sent to sentry.我正在使用 django 的 LOGGING 设置,但目前我不在乎这些消息是“错误”还是以其他方式发送给哨兵。 My main goal at the moment is to catch and send any exception that occurs to sentry.我目前的主要目标是捕获并发送发生在哨兵身上的任何异常。

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,
        }
    },
}

UPDATE更新

I've managed to get the app1.views.error view to report an error to sentry when using runserver locally, but after updating the configuration and deploying it's still not working when deployed.我已经设法让app1.views.error视图在本地使用runserver时向哨兵报告错误,但是在更新配置和部署后,它在部署时仍然无法正常工作。 ( manage raven test does) manage raven test确实如此)

From the sentry docs来自哨兵文档

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,
    },
},

} }

Link to doc: https://docs.sentry.io/clients/python/integrations/django/链接到文档: https : //docs.sentry.io/clients/python/integrations/django/

EDIT: From the comment编辑:来自评论

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

if you don't want to do this manually and want sentry logging to occur anytime an exception block is triggered anywhere in your project, use the logging based solution above.如果您不想手动执行此操作并希望在项目中的任何位置触发异常块时都发生哨兵日志记录,请使用上面基于日志记录的解决方案。

As @Mehran said after configuration of your LOGGING in settings.py , you will be able to use logging.正如@Mehran 在settings.py配置您的LOGGING后所说,您将能够使用日志记录。 Let's assume that you have example in loggers in configuration like;让我们假设您在配置中的loggers中有example

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

Then;然后;

import logging

logger = logging.getLogger("example")

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

logger also have warn , info etc. logger也有warninfo等。

It is also possible to post directly to your Sentry DSN without using any of the Sentry libraries, but using pure requests .也可以不使用任何 Sentry 库而直接发布到您的 Sentry DSN,而是使用纯requests This can be useful if you're running a version of Python or Django that isn't compatible with Sentry's latest DSN.如果您运行的 Python 或 Django 版本与 Sentry 的最新 DSN 不兼容,这会很有用。

I've posted a code for this -including an example test-here我已经为此发布了一个代码 - 包括一个示例测试 -这里

Just checking in to say that the other answers are totally outdated.只是检查说其他答案已经完全过时了。

The raven library has been deprecated in favor of sentry-sdk and setting this up does not involve mangling the logging configuration anymore, since a default logging integration will already spy on any logger call. raven 库已被弃用,取而代之的是 sentry-sdk 并且设置它不再涉及修改日志配置,因为默认的日志集成已经可以监视任何记录器调用。

Just do做就是了

import sentry_sdk.integrations.django

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

in your settings.py and you are good to go (remember to export a SENTRY_SDN variable and whatnot).在你的 settings.py 中,你很高兴(记得导出一个 SENTRY_SDN 变量等等)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM