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 --upgrade 'sentry-sdk[loguru]'

Add LoguruIntegration() to your integrations list:

Copied
import sentry_sdk
from sentry_sdk.integrations.loguru import LoguruIntegration

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    integrations=[
        LoguruIntegration(),
    ],
)

Copied
from loguru import logger

def main():
    sentry_sdk.init(...)  # same as above
    logger.debug("I am ignored")
    logger.error("There was an error!")

main()

This will capture the error level log entry and send it as an error to Sentry.

By default, logs with a level of INFO or higher will be added as breadcrumbs to Sentry events. Sentry issue will be created for logs with a level of ERROR or higher:

Copied
from loguru import logger

logger.debug("I am ignored")
logger.info("I am a breadcrumb")
logger.error("I am an event", extra=dict(bar=43))
logger.exception("An exception happened")
  • An error event with the message "I am an event" will be created.
  • "I am a breadcrumb" will be attached as a breadcrumb to that event.
  • bar will end up in the extra attributes of that event.
  • "An exception happened" 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 am ignored" will not be captured by Sentry. To capture it, set level to DEBUG or lower in LoguruIntegration.

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")  # No error is sent to Sentry

This will work with logging's logger too

Copied
logger = logging.getLogger("a.spammy.logger")
logger.error("hi") # Again, 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.

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_loguru = LoguruIntegration(
    level=LoggingLevels.INFO.value,        # Capture info and above as breadcrumbs
    event_level=LoggingLevels.ERROR.value  # Send errors as events
)

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    integrations=[
        sentry_loguru,
    ],
)
  • level

    The Sentry Python SDK will record log records with a level higher than or equal to level as breadcrumbs. Inversely, the SDK ignores any log record 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

  • Loguru: 0.5+
  • Python: 3.6+

The versions above apply for Sentry Python SDK version 2.0+, which drops support for some legacy Python and framework versions. If you're looking to use Sentry with older Python or framework versions, consider using an SDK version from the 1.x major line of releases.

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").