Logging
Learn about logging with Python.
Adds support for Python logging.
Install sentry-sdk
from PyPI:
pip install --upgrade sentry-sdk
The logging integrations is a default integration so it will be enabled automatically when you initialize the Sentry SDK.
import sentry_sdk
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0"
)
import logging
def main():
sentry_sdk.init(...) # same as above
logging.debug("I am ignored")
logging.info("I am a breadcrumb")
logging.error("I am an event", extra=dict(bar=43))
logging.exception("An exception happened")
main()
- 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'sextra
attributes."An exception happened"
will send the current exception fromsys.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 lowerlevel
toDEBUG
(See below).
To change the default behavior of the logging integration, instantiate the integration manually and pass it to Sentry's init
function:
import logging
import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration
# The SDK will honor the level set by the logging library, which is WARNING by default.
# If we want to capture records with lower severity, we need to configure
# the logger level first.
logging.basicConfig(level=logging.INFO)
sentry_sdk.init(
# ...
integrations=[
LoggingIntegration(
level=logging.INFO, # Capture info and above as breadcrumbs
event_level=logging.INFO # Send records as events
),
],
)
You can pass the following keyword arguments to LoggingIntegration()
:
level
(defaultINFO
): The Sentry Python SDK will record log records with a level higher than or equal tolevel
as breadcrumbs. Inversely, the SDK completely ignores any log record with a level lower than this one. If a value ofNone
occurs, the SDK won't send log records as breadcrumbs.event_level
(defaultERROR
): The Sentry Python SDK will report log records with a level higher than or equal toevent_level
as events as long as the logger itself is set to output records of those log levels (see note below). If a value ofNone
occurs, the SDK won't send log records as events.
Note
The Sentry Python SDK will honor the configured level of each logger (set with logger.setLevel(level)
or logging.basicConfig(level=level)
). That means that you will not see any INFO
or DEBUG
events from a logger with the level set to WARNING
, regardless of how you configure the integration. If not set explicitly, the logging level defaults to WARNING
.
Sometimes a logger is extremely noisy and spams you with pointless errors. You can ignore that logger by calling ignore_logger
:
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-send
and before-breadcrumb
to ignore only certain messages. See Filtering Events for more information.
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 capture data will miss errors you may not think of on your own.
See the API documentation for more information.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").