sentry_sdk.init() already integrates with the logging module. It is
equivalent to this explicit configuration:
import logging import sentry_sdk from sentry_sdk.integrations.logging import LoggingIntegration # All of this is already happening by default! sentry_logging = LoggingIntegration( level=logging.INFO, # Capture info and above as breadcrumbs event_level=logging.ERROR # Send errors as events ) sentry_sdk.init( dsn="___PUBLIC_DSN___", integrations=[sentry_logging] )
import logging logging.debug("I am ignored") logging.info("I am a breadcrumb") logging.error("I am an event", extra=dict(bar=43)) logging.error("An exception happened", exc_info=True)
- 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.
barwill end up in the event’s
"An exception happened"will send the current exception from
sys.exc_info()with the stack trace and everything to the Sentry Python SDK. If there’s no exception, the current stack will be attached.
- The debug message
"I am ignored"will not surface anywhere. To capture it, you need to lower
(New in version 0.5.0: Ability to add data to
(New in version 0.6.0:
exc_info=True now always attaches a stack trace)
Ignoring a logger
Sometimes a logger is extremely noisy and spams you with pointless errors. You can completely ignore that logger by calling
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
You can also use
before-breadcrumb to ignore
only certain messages. See Filtering Events for more information.
You can pass the following keyword arguments to
INFO): The Sentry Python SDK will record log records with a level higher than or equal to
levelas breadcrumbs. Inversely, the SDK completely ignores any log record with a level lower than this one. If a value of
Noneoccurs, the SDK won’t send log records as breadcrumbs.
ERROR): The Sentry Python SDK will report log records with a level higher than or equal to
event_levelas events. If a value of
Noneoccurs, the SDK won’t send log records as events.
Instead of using
LoggingIntegration, you can use two regular logging
logging.Handler subclasses that the integration exports.
Usually, you don’t need this. You can use this together with
default_integrations=False if you want to opt into what the Sentry Python SDK captures. However, correctly setting up logging is difficult. Also, an opt-in approach to capturing data will miss errors you may not think of on your own.
See the API documentation for more information.