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.
pip install "sentry-sdk[loguru]"
If you have the loguru package in your dependencies, the Loguru integration will be enabled automatically when you initialize the Sentry SDK.
import sentry_sdk
sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0example-org / example-project",
    # 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,
)
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:
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.
- barwill end up in the- extraattributes 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, setleveltoDEBUGor lower inLoguruIntegration.
Loguru log records can additionally also be captured as Sentry logs as long as the enable_logs option is True.
import sentry_sdk
from loguru import logger
sentry_sdk.init(
    # ...
    enable_logs=True,
)
logger.info("I will be sent to Sentry logs")
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:
# Import form `logging` integration
from sentry_sdk.integrations.logging import ignore_logger
ignore_logger("a.spammy.logger")
In a.spammy.logger module:
from loguru import logger
logger.error("hi")  # No error is sent to Sentry
This will work with logging's logger too
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():
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(
            level=LoggingLevels.INFO.value,              # Capture INFO and above as breadcrumbs
            event_level=LoggingLevels.ERROR.value,       # Send ERROR logs as events
            sentry_logs_level=LoggingLevels.INFO.value,  # Capture INFO and above as logs
        )
    ],
)
- level- The Sentry Python SDK will record log records with a level higher than or equal to - levelas 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_levelas events. If set to- None, the SDK won't send log records as events.- Default: - ERROR
- sentry_logs_level- The Sentry Python SDK will capture log records with a level higher than or equal to - sentry_logs_levelas 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_logsoption when initializing the SDK (regardless of the- sentry_logs_levelsetting).Copied- sentry_sdk.init( # ... enable_logs=True, )- Default: - INFO
- Loguru: 0.5+
- Python: 3.6+
The versions above apply for the current major version of the Python SDK. If you're looking to use Sentry with older Python or framework versions, consider using an older major version of the SDK.
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").