The Loguru integration lets you capture log messages and send them to Sentry.
logging integration provides most of the Loguru functionality and most examples on that page work with Loguru.
sentry-sdk from PyPI with the
pip install --upgrade 'sentry-sdk[loguru]'
LoguruIntegration() to your integrations list:
import sentry_sdk from sentry_sdk.integrations.loguru import LoguruIntegration sentry_sdk.init( dsn="https://examplePublicKey@o0.ingest.sentry.io/0", integrations=[ LoguruIntegration(), ], )
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, set
DEBUGor lower in
Loggers can be noisy. You can ignore a logger by calling
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")
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-breadcrumb to ignore only certain messages. See Filtering Events for more information.
You can pass the following keyword arguments to
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, ], )
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.
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.
- Loguru: 0.5+
- Python: 3.5+