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"

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 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")
  • All of the above logs except for the DEBUG level 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.
  • bar will end up in the extra attributes of that event.
  • "I'm an exception log" will send the current exception from sys.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 the level option to adjust which levels should be sent to Sentry as logs, and the breadcrumb_level option 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
    }
)

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
        ),
    ],
)

You can pass the following keyword arguments to LoggingIntegration():

  • level (default INFO): The SDK captures records with a level higher than or equal to level as Sentry structured logs. If set to None, the SDK won't send records as Sentry logs.

  • breadcrumb_level (default INFO): The SDK records log records with a level higher than or equal to breadcrumb_level as breadcrumbs. If set to None, the SDK won't record log records as breadcrumbs.

  • event_level (default None): The SDK reports log records with a level higher than or equal to event_level as events, as long as the logger itself is set to output records of those log levels (see Troubleshooting). If set to None, 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

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

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.

Logs not appearing in Sentry

First, make sure you're on the latest version of the SDK. If you're using the sentry_sdk.logger API directly (e.g., sentry_sdk.logger.info(...)), logs are sent automatically. If you're using Python's logging module and want those log records captured as Sentry logs, make sure you've added LoggingIntegration() to your integrations and that its level option isn't set to None.

The SDK will honor the configured level of each logger (set with logger.setLevel(level) or logging.basicConfig(level=level)). That means you won't 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.

Some frameworks, such as uvicorn, may reset or override your logging configuration. To ensure your log level is applied and logs at or above the configured level are picked up by the SDK, you can pass force=True to logging.basicConfig(), for example logging.basicConfig(level=level, force=True).

Error events no longer created from logs

Starting with SDK 3.0, the logging integration doesn't create error events from log records by default. Set event_level=logging.ERROR on LoggingIntegration() to create error events from ERROR logs and higher.

Was this helpful?
Help improve this content
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").