Loguru

Learn about using Sentry with Loguru.

The Loguru integration lets you capture log messages and send them to Sentry.

The logging integration provides most of the Loguru functionality and most examples on that page work with Loguru.

Install sentry-sdk from PyPI with the loguru extra.

Copied
pip install "sentry-sdk[loguru]"

To capture Loguru log records as Sentry logs, set enable_logs to True. The integration itself doesn't need to be added manually. If you have the loguru package in your dependencies, the Loguru integration will be enabled automatically when you initialize the Sentry SDK.

Copied
import sentry_sdk

sentry_sdk.init(
    dsn="___PUBLIC_DSN___",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    enable_logs=True,
)

Copied
from loguru import logger

def main():
    sentry_sdk.init(...)  # same as above
    logger.info("Logging some info")
    logger.error("Logging an error")

main()

This will capture both logs and send them to Sentry Logs. Additionally, an error event will be created from the ERROR-level log. In addition to that, a breadcrumb will be created from the INFO-level log.

Logs with a level of INFO and higher will be captured as Sentry logs as long as enable_logs is True and the log level set in the logging module is INFO or below. The threshold can be configured via the sentry_logs_level option.

Additionally, the Loguru integration will create an error event from all ERROR-level logs. This feature is configurable via the event_level integration option.

INFO and above logs will also be captured as breadcrumbs. Use the level integration option to adjust the threshold.

The following snippet demonstrates the default behavior:

Copied
import sentry_sdk
from loguru import logger

sentry_sdk.init(
    ...,
    enable_logs=True,
)

# The following will be captured as Sentry logs:
logger.info("I'm an INFO log")
logger.error("I'm an ERROR log", extra={"bar": 43})
logger.exception("I'm an exception log")

# DEBUG-level logs won't be captured by default
logger.debug("I'm a DEBUG log")
  • All of the above logs except for the DEBUG-level message will be 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" will not be captured by Sentry. See the sentry_logs_level option to adjust which log levels should be sent to Sentry as logs, and the level option to adjust the level for capturing breadcrumbs.

Loggers can be noisy. You can ignore a logger by calling ignore_logger.

Since most of the logic is proxied to logging integration, we use it instead of the Loguru integration:

Copied
# Import form `logging` integration
from sentry_sdk.integrations.logging import ignore_logger

ignore_logger("a.spammy.logger")

In a.spammy.logger module:

Copied
from loguru import logger
logger.error("hi")  # Nothing is sent to Sentry

This will work with logging's logger too

Copied
logger = logging.getLogger("a.spammy.logger")
logger.error("hi") # Again, nothing is sent to Sentry

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.

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

Copied
import sentry_sdk
from loguru import logger

from sentry_sdk.integrations.loguru import LoguruIntegration
from sentry_sdk.integrations.loguru import LoggingLevels

sentry_sdk.init(
    # ...
    integrations=[
        LoguruIntegration(
            sentry_logs_level=LoggingLevels.INFO.value,  # Capture INFO and above as logs
            level=LoggingLevels.INFO.value,              # Capture INFO and above as breadcrumbs
            event_level=LoggingLevels.ERROR.value,       # Send ERROR logs as events
        )
    ],
)
  • sentry_logs_level

    The Sentry Python SDK will capture log records with a level higher than or equal to sentry_logs_level as Sentry structured logs. If set to None, the SDK won't send records as logs.

    To capture Loguru log records as Sentry logs, you must enable the enable_logs option when initializing the SDK (regardless of the sentry_logs_level setting).

    Copied
    sentry_sdk.init(
        # ...
        enable_logs=True,
    )
    

    Default: INFO

  • level

    The Sentry Python SDK will record log records with a level higher than or equal to level as breadcrumbs. Inversely, the SDK will not capture breadcrumbs for logs with a level lower than this one. If set to None, the SDK won't send log records as breadcrumbs.

    Default: INFO

  • event_level

    The Sentry Python SDK will report log records with a level higher than or equal to event_level as events. If set to None, the SDK won't send log records as events.

    Default: ERROR

Logs not appearing in Sentry

First, make sure you have the enable_logs=True option in your sentry_sdk.init() and you're on the latest version of the SDK.

Your logs could be missing because of the logging level of the logger. The SDK will honor the configured level of each logger. That means that you won't see any INFO or DEBUG data in Sentry from a logger with the level set to WARNING, regardless of how you configure the integration.

It might also be the case that the SDK's Loguru setup is being overwritten in your app. Sentry plugs into Loguru by adding its own handlers when you call sentry_sdk.init(). If those handlers are later overridden, for example by calling logger.configure(handlers=...), the integration won't work.

Another reason why you might not be seeing your logs in Sentry is because they were rejected client- or server-side. This could be due to them being too large, or because of reasons like rate limits or backpressure management. In Sentry, you can go to Settings > Stats & Usage and select Logs from the Category dropdown to see what logs were dropped.

  • Loguru: 0.5+
  • Python: 3.6+
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").