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.
Sentry Logs for Loguru
Enable the Sentry Logs feature with sentry_sdk.init(enable_logs=True) to unlock Sentry's full logging power. With Sentry Logs, you can search, filter, and analyze logs from across your entire application in one place.
Install sentry-sdk from PyPI with the loguru extra.
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.
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,
)
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:
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.barwill end up in theextraattributes of that event."I'm an exception log"will send the current exception fromsys.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 thesentry_logs_leveloption to adjust which log levels should be sent to Sentry as logs, and theleveloption 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:
# 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") # Nothing is sent to Sentry
This will work with logging's logger too
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():
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_levelThe Sentry Python SDK will capture log records with a level higher than or equal to
sentry_logs_levelas Sentry structured logs. If set toNone, 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 thesentry_logs_levelsetting).Copiedsentry_sdk.init( # ... enable_logs=True, )Default:
INFOlevelThe Sentry Python SDK will record log records with a level higher than or equal to
levelas breadcrumbs. Inversely, the SDK will not capture breadcrumbs for logs with a level lower than this one. If set toNone, the SDK won't send log records as breadcrumbs.Default:
INFOevent_levelThe Sentry Python SDK will report log records with a level higher than or equal to
event_levelas events. If set toNone, the SDK won't send log records as events.Default:
ERROR
- 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").