简体   繁体   English

如何防止哨兵在 Django 本地服务器上记录错误?

[英]How to prevent sentry from logging errors while on Django local server?

I am trying to prevent Sentry from logging errors to my Sentry dashboard while I'm working on my local server (ie http://127.0.0.1:8000/ ).当我在本地服务器上工作时(即http://127.0.0.1:8000/ ),我试图阻止 Sentry 将错误记录到我的 Sentry 仪表板。 The only time I want sentry to log errors to my dashboard is when my code is in production.我希望哨兵将错误记录到仪表板的唯一时间是我的代码在生产中。 How can I go about this?我怎么能 go 关于这个? I have tried this below, but it doesn't work:我在下面尝试过,但它不起作用:

if DEBUG == True
    sentry_sdk.init(
        dsn=os.environ.get('SENTRY_DSN', None),
        integrations=[DjangoIntegration()],
    
        # Set traces_sample_rate to 1.0 to capture 100%
        # of transactions for performance monitoring.
        # We recommend adjusting this value in production.
        traces_sample_rate=1.0,
    
        # If you wish to associate users to errors (assuming you are using
        # django.contrib.auth) you may enable sending PII data.
        send_default_pii=True
    )

There are two ways you can do this:有两种方法可以做到这一点:

The first option is to import sys and check for runserver (ref: https://stackoverflow.com/a/49874564/15205504 )第一个选项是import sys并检查runserver (参考: https://stackoverflow.com/a/49874564/15205504

import sys

if (len(sys.argv) >= 2 and sys.argv[1] != 'runserver'):
    sentry_sdk.init(
        dsn=os.environ.get('SENTRY_DSN', None),
        integrations=[DjangoIntegration()],

        # Set traces_sample_rate to 1.0 to capture 100%
        # of transactions for performance monitoring.
        # We recommend adjusting this value in production.
        traces_sample_rate=1.0,

        # If you wish to associate users to errors (assuming you are using
        # django.contrib.auth) you may enable sending PII data.
        send_default_pii=True
)

The second option is to specify an environment type in your settings.py .第二个选项是在settings.py中指定环境类型。 For instance, if your production server is Heroku, you can create an env_type variable in Heroku or your .env file and set it to 'HEROKU' , then use it like this:例如,如果您的生产服务器是 Heroku,您可以在 Heroku 或.env文件中创建一个 env_type 变量并将其设置为'HEROKU' ,然后像这样使用它:

env_type = os.environ.get('env_type', 'LOCAL')

if env_type == 'HEROKU':
    sentry_sdk.init(
        dsn=os.environ.get('SENTRY_DSN', None),
        integrations=[DjangoIntegration()],

        # Set traces_sample_rate to 1.0 to capture 100%
        # of transactions for performance monitoring.
        # We recommend adjusting this value in production.
        traces_sample_rate=1.0,

        # If you wish to associate users to errors (assuming you are using
        # django.contrib.auth) you may enable sending PII data.
        send_default_pii=True
)

Try like this:试试这样:

  if not DEBUG:
    sentry_sdk.init(
        dsn="SENTRY_DSN",
        integrations=[DjangoIntegration()],

        # Set traces_sample_rate to 1.0 to capture 100%
        # of transactions for performance monitoring.
        # We recommend adjusting this value in production.
        traces_sample_rate=1.0,

        # If you wish to associate users to errors (assuming you are using
        # django.contrib.auth) you may enable sending PII data.
        send_default_pii=True
    )

Setting the dsn to None will no-op all SDK operations, so your config should work unless you are also setting the SENTRY_DSN variable in your local environment.dsn设置为None将不执行所有 SDK 操作,因此除非您还在本地环境中设置SENTRY_DSN变量,否则您的配置应该可以工作。

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

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