Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Sentry Python SDK中忽略记录器_Python_Django_Sentry - Fatal编程技术网

如何在Sentry Python SDK中忽略记录器

如何在Sentry Python SDK中忽略记录器,python,django,sentry,Python,Django,Sentry,我正在使用sentry pythonSDK从django服务器捕获异常 我不想像上面那样捕获django.security.DisallowedHost。 如何移除该记录器的岗哨操作 我在下面附上了我的服务器配置 settings.py LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'null': { 'level': 'DEBU

我正在使用
sentry python
SDK从django服务器捕获异常

我不想像上面那样捕获
django.security.DisallowedHost
。 如何移除该记录器的岗哨操作

我在下面附上了我的服务器配置

settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
       'null': {
            'level': 'DEBUG',
            'class': 'logging.NullHandler',
        },
    },
    'loggers': {
        # Silence SuspiciousOperation.DisallowedHost exception ('Invalid
        # HTTP_HOST' header messages). Set the handler to 'null' so we don't
        # get those annoying emails.
        'django.security.DisallowedHost': {
            'handlers': ['null'],
            'propagate': False,
        },
    }
}

sentry_sdk.init(
    dsn=os.environ['SENTRY_DSN'],
    integrations=[DjangoIntegration()],
    send_default_pii=True,
    release=f"{os.environ['STAGE']}@{os.environ['VERSION']}",
)
快速回答

通过某些特征忽略事件的一种更复杂但通用的方法 看


在sentry_sdk下,我必须在发送前使用以下代码,以使其忽略django.security.DisallowedHost异常

def before_send(event, hint):
        """Don't log django.DisallowedHost errors in Sentry."""
        if 'log_record' in hint:
            if hint['log_record'].name == 'django.security.DisallowedHost':
                return None

        return event

1.对不起,我的问题有点含糊不清<代码>安全性。不允许的主机也不例外。这正是Django的记录者。如何使用DjangoIntegration忽略sentry中的某些日志记录程序?@kde713我修复了我的答案,请针对ignore_错误提出单独的问题,以保持此站点的干净性此代码不起作用。我添加了您的代码并将
a.spammy.Logger
替换为
django.security.DisallowedHost
。但是无效的HTTP_主机仍然发送给sentry。关于编辑,我刚刚通过了:那是我修改了我自己的答案,但我忘记登录了。较短的方法做同样的事情。
import sentry_sdk

def before_breadcrumb(crumb, hint):
    if crumb.get('category', None) == 'a.spammy.Logger':
        return None
    return crumb

def before_send(event, hint):
    if event.get('logger', None) == 'a.spammy.Logger':
        return None
    return event

sentry_sdk.init(before_breadcrumb=before_breadcrumb, before_send=before_send)
def before_send(event, hint):
        """Don't log django.DisallowedHost errors in Sentry."""
        if 'log_record' in hint:
            if hint['log_record'].name == 'django.security.DisallowedHost':
                return None

        return event