Logging for Python
Learn about logging with Python.
The logging integration adds support for the logging framework from Python's standard library. Depending on your settings, logs can be captured as Sentry logs, as error events, or as breadcrumbs, or a combination of those.
This page describes the logging integration in version 3.x of the Python SDK. For 2.x, switch to 2.x in the version selector.
Sentry Logs
Add LoggingIntegration() to your integrations to send log records as Sentry logs. With Sentry Logs, you can search, filter, and analyze logs from across your entire application in one place.
Install sentry-sdk from PyPI:
pip install "sentry-sdk"
pip install "sentry-sdk"
uv add "sentry-sdk"
To enable the integration, add LoggingIntegration() to your integrations list:
import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
# Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
data_collection={
"user_info": True,
},
integrations=[
LoggingIntegration(),
],
)
import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
# Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
data_collection={
"user_info": True,
},
integrations=[
LoggingIntegration(),
],
)
import logging
def main():
sentry_sdk.init(...) # same as above
logging.info("Logging some info")
logging.error("Logging an error")
main()
import logging
def main():
sentry_sdk.init(...) # same as above
logging.info("Logging some info")
logging.error("Logging an error")
main()
This sends log records to Sentry as Sentry logs and records them as breadcrumbs.
By default, logs with a level of INFO and higher are captured as Sentry logs, as long as the log level set in the logging module is INFO or below. Use the level option to change the threshold.
INFO and above logs are also recorded as breadcrumbs. Use the breadcrumb_level option to change the threshold.
The integration doesn't create error events from log records by default. To create error events from ERROR logs and higher, set the event_level option to logging.ERROR.
import logging
import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration
sentry_sdk.init(
...,
integrations=[
LoggingIntegration(event_level=logging.ERROR),
],
)
# The following will be captured as Sentry logs:
logging.info("I'm an INFO log")
logging.error("I'm an ERROR log", extra={"bar": 43})
logging.exception("I'm an exception log")
# DEBUG-level logs won't be captured by default
logging.debug("I'm a DEBUG log")
import logging
import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration
sentry_sdk.init(
...,
integrations=[
LoggingIntegration(event_level=logging.ERROR),
],
)
# The following will be captured as Sentry logs:
logging.info("I'm an INFO log")
logging.error("I'm an ERROR log", extra={"bar": 43})
logging.exception("I'm an exception log")
# DEBUG-level logs won't be captured by default
logging.debug("I'm a DEBUG log")
- All of the above logs except for the
DEBUGlevel message are sent to Sentry as logs. - An error event with the message
"I'm an ERROR log"will be created. "I'm an INFO log"will be attached as a breadcrumb to that event.barwill end up in theextraattributes of that event."I'm an exception log"will send the current exception fromsys.exc_info()with the stack trace to Sentry. If there's no exception, the current stack will be attached.- The debug message
"I'm a DEBUG log"isn't captured. See theleveloption to adjust which levels should be sent to Sentry as logs, and thebreadcrumb_leveloption to adjust the level for capturing breadcrumbs.
When sending log records as Sentry logs, any fields provided in the extra dictionary are automatically promoted to top-level attributes on the log entry. This makes them searchable and filterable in the Sentry UI.
import logging
import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration
sentry_sdk.init(
# ...
integrations=[
LoggingIntegration(),
],
)
logger = logging.getLogger(__name__)
# Extra fields become top-level searchable attributes
logger.error(
"Payment processing failed",
extra={
"user_id": 12345,
"transaction_id": "txn_abc123",
"payment_method": "credit_card",
"amount": 99.99
}
)
import logging
import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration
sentry_sdk.init(
# ...
integrations=[
LoggingIntegration(),
],
)
logger = logging.getLogger(__name__)
# Extra fields become top-level searchable attributes
logger.error(
"Payment processing failed",
extra={
"user_id": 12345,
"transaction_id": "txn_abc123",
"payment_method": "credit_card",
"amount": 99.99
}
)
In this example, user_id, transaction_id, payment_method, and amount will appear as separate, searchable attributes in the Sentry logs interface, not nested within an extra object. You can filter and query logs using these attributes directly, such as user_id:12345 or payment_method:credit_card.
To change the default behavior of the logging integration, pass options to LoggingIntegration():
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 logs
breadcrumb_level=logging.INFO, # Capture INFO and above as breadcrumbs
event_level=logging.ERROR, # Send ERROR records as events
),
],
)
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 logs
breadcrumb_level=logging.INFO, # Capture INFO and above as breadcrumbs
event_level=logging.ERROR, # Send ERROR records as events
),
],
)
You can pass the following keyword arguments to LoggingIntegration():
level(defaultINFO): The SDK captures records with a level higher than or equal tolevelas Sentry structured logs. If set toNone, the SDK won't send records as Sentry logs.breadcrumb_level(defaultINFO): The SDK records log records with a level higher than or equal tobreadcrumb_levelas breadcrumbs. If set toNone, the SDK won't record log records as breadcrumbs.event_level(defaultNone): The SDK reports log records with a level higher than or equal toevent_levelas events, as long as the logger itself is set to output records of those log levels (see Troubleshooting). If set toNone, the SDK won't send log records as events.
To stop the SDK from sending a noisy logger's records as Sentry logs, call 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 Sentry log sent
from sentry_sdk.integrations.logging import ignore_logger
ignore_logger("a.spammy.logger")
logger = logging.getLogger("a.spammy.logger")
logger.error("hi") # no Sentry log sent
To stop a logger from creating events and breadcrumbs, call ignore_logger_for_events:
from sentry_sdk.integrations.logging import ignore_logger_for_events
ignore_logger_for_events("a.spammy.logger")
logger = logging.getLogger("a.spammy.logger")
logger.error("hi") # no error event or breadcrumb created
from sentry_sdk.integrations.logging import ignore_logger_for_events
ignore_logger_for_events("a.spammy.logger")
logger = logging.getLogger("a.spammy.logger")
logger.error("hi") # no error event or breadcrumb created
To ignore a logger completely, call both functions. Use unignore_logger and unignore_logger_for_events to revert them.
You can also use before_send_log (for Sentry logs), before_send (for Sentry errors) and before_breadcrumb (for breadcrumbs) to ignore only certain messages. See Filtering Events for more information.
Instead of using LoggingIntegration, you can attach the logging.Handler subclasses that the integration exports to your loggers yourself: LogsHandler (for Sentry logs), EventHandler (for error events), and BreadcrumbHandler (for breadcrumbs).
Usually, you don't need this. Correctly setting up logging is difficult, and attaching handlers to specific loggers may miss records you didn't think of.
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").