As recommended by Sentry's docs [1][2] for their new unified python sdk (sentry_sdk
), I've configured it with my Django application to capture events on all exceptions or "error"-level logs:
import sentry_sdk
import logging
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.logging import LoggingIntegration
sentry_logging = LoggingIntegration(
level=logging.DEBUG,
event_level=logging.ERROR
)
sentry_sdk.init(
dsn="{{sentry_dsn}}",
integrations=[DjangoIntegration(), sentry_logging]
)
However, since this hooks directly into python's logging module and internal exception handling, it means anything that uses this Django environment will be shipping events to sentry. There are some tasks (such as interactive manage.py commands, or work in the REPL) that need the Django environment, but for which I don't want events created in Sentry.
Is there a way to indicate to sentry that I'd like it to not capture events from exceptions or logging
calls for the current task? Or a way to temporarily disable it after it's been globally configured?
The Sentry SDK contains several methods that you can use to explicitly report errors, events, and custom messages in except clauses, critical areas of your code, and so on. Open the views.py file. Notice that we import sentry_sdk lib which contains the capture_exception method:
Logging integrations, commonly referred to as appenders, used to translate log records generated by your application via a logging framework into Sentry events. These integrations are packaged as sentry-logback , sentry-log4j, and sentry-log4j2. Sentry, used by the logging integrations to send events to a Sentry server.
There will be an error event with the message "I am an event". "I am a breadcrumb" will be attached as a breadcrumb to that event. bar will end up in the event's extra attributes. "An exception happened" will send the current exception from sys.exc_info () with the stack trace and everything to the Sentry Python SDK.
event_level (default ERROR ): The Sentry Python SDK will report log records with a level higher than or equal to event_level as events. If a value of None occurs, the SDK won't send log records as events.
You can run sentry_sdk.init()
(notably without a DSN) again to disable the SDK.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With