Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore a logger in the Sentry Python SDK

I'm using sentry-python SDK for capture exceptions from my django server.

sentry capture

I don't want to capture django.security.DisallowedHost like above. How to remove sentry handling for that logger?

I attached my server configuration below.

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']}",
)
like image 464
kde713 Avatar asked Oct 22 '18 10:10

kde713


People also ask

What is logger exception in Python?

Logging an exception in python with an error can be done in the logging. exception() method. This function logs a message with level ERROR on this logger. The arguments are interpreted as for debug(). Exception info is added to the logging message.

Can Sentry be used for logs?

By using a tool like Sentry along with our logs and metrics, we're not only able to know what's going on much faster, but we're able to put out fires faster.

How do I check Sentry logs?

The View Module Logs section in the Troubleshooting > Logs page contains links to logs for Sentry modules. In the View Module Logs section, click the link for the log you want to view.


2 Answers

Quick answer

See LoggingIntegration, eg:

from sentry_sdk.integrations.logging import ignore_logger


ignore_logger("a.spammy.logger")

logger = logging.getLogger("a.spammy.logger")
logger.error("hi")  # no error sent to sentry

A more elaborate but generic way to ignore events by certain characteristics

See before_breadcrumb and before_send, eg:

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)
like image 108
Markus Unterwaditzer Avatar answered Sep 28 '22 17:09

Markus Unterwaditzer


Under the sentry_sdk I had to use the following code in before_send to get it to ignore the django.security.DisallowedHost exception.

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
like image 45
kwiersma Avatar answered Sep 28 '22 18:09

kwiersma