Skip to content

feat(python): Add Loguru to Sentry logs docs #14028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion docs/platforms/python/integrations/logging/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ main()
- `"An exception happened"` will send the current exception from `sys.exc_info()` with the stack trace and everything to the Sentry Python SDK. If there's no exception, the current stack will be attached.
- The debug message `"I am ignored"` will not surface anywhere. To capture it, you need to lower `level` to `DEBUG` (See below).

Log records can additionally also be captured as [Sentry logs](/platforms/python/logs/) as long as the `enable_logs` experimental option is `True`.

```python
import logging
import sentry_sdk

sentry_sdk.init(
# ...
_experiments={
"enable_logs": True,
},
)

logging.info("I will be sent to Sentry logs")
```

## Options

To change the default behavior of the logging integration, instantiate the integration manually and pass it to Sentry's `init` function:
Expand Down Expand Up @@ -86,7 +102,7 @@ You can pass the following keyword arguments to `LoggingIntegration()`:

- `event_level` (default `ERROR`): The Sentry Python SDK will report log records with a level higher than or equal to `event_level` as events as long as the logger itself is set to output records of those log levels (see note below). If a value of `None` occurs, the SDK won't send log records as events.

- `sentry_logs_level` (default `INFO`): The Sentry Python SDK will capture records with a level higher than or equal to `sentry_logs_level` as logs as long as the `enable_logs` experimental option is `True`:
- `sentry_logs_level` (default `INFO`): The Sentry Python SDK will capture records with a level higher than or equal to `sentry_logs_level` as [Sentry structured logs](/platforms/python/logs/) as long as the `enable_logs` experimental option is `True`.

```python
sentry_sdk.init(
Expand Down
18 changes: 17 additions & 1 deletion docs/platforms/python/integrations/loguru/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ logger.exception("An exception happened")
- `"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`.

Loguru log records can additionally also be captured as [Sentry logs](/platforms/python/logs/) as long as the `enable_logs` experimental option is `True`.

```python
import sentry_sdk
from loguru import logger

sentry_sdk.init(
# ...
_experiments={
"enable_logs": True,
},
)

logger.info("I will be sent to Sentry logs")
```

### Ignoring a logger

Loggers can be noisy. You can ignore a logger by calling `ignore_logger`.
Expand Down Expand Up @@ -134,7 +150,7 @@ sentry_sdk.init(

- `sentry_logs_level`

The Sentry Python SDK will capture log records with a level higher than or equal to `sentry_logs_level` as logs. If set to `None`, the SDK won't send records as logs.
The Sentry Python SDK will capture log records with a level higher than or equal to `sentry_logs_level` as [Sentry structured logs](/platforms/python/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 experimental `enable_logs` option when initializing the SDK (regardless of the `sentry_logs_level` setting).

Expand Down
88 changes: 76 additions & 12 deletions platform-includes/logs/integrations/python.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### Python Logger Integration
### Standard library logging

Sentry automatically instruments Python loggers via its `LoggingIntegration`.
The SDK's `LoggingIntegration` instruments standard library loggers in order to send Sentry structured logs to Sentry.

```python
import sentry_sdk
Expand All @@ -9,8 +9,8 @@ import logging
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
_experiments={
"enable_logs": True
}
"enable_logs": True,
},
)

# Your existing logging setup
Expand All @@ -22,7 +22,7 @@ my_logger.debug('In this example debug events will not be sent to Sentry logs. m
my_logger.info('But info events will be sent to Sentry logs. my_value=%s', my_value)
```

By default, the logging integration sends INFO-level logs and higher to Sentry logs. You can set a different threshold via the `LoggingIntegration`'s `sentry_logs_level` parameter. However, regardless of the `sentry_logs_level` setting, the SDK only sends logs if they are at or above the logger's level.
By default, the logging integration sends `INFO`-level logs and higher to Sentry logs. You can set a different threshold via the `LoggingIntegration`'s `sentry_logs_level` parameter. However, regardless of the `sentry_logs_level` setting, the SDK only sends logs if they are at or above the logger's level.

```python
import sentry_sdk
Expand Down Expand Up @@ -61,15 +61,79 @@ from sentry_sdk.integrations.logging import SentryLogsHandler
from sentry_sdk.integrations.logging import LoggingIntegration

sentry_sdk.init(
dsn="...",
_experiments={
"enable_logs": True
},
integrations=[
LoggingIntegration(sentry_logs_level=None), # Do not monkeypatch the sentry handler
]
dsn="___PUBLIC_DSN___",
_experiments={
"enable_logs": True
},
integrations=[
LoggingIntegration(sentry_logs_level=None), # Do not monkeypatch the sentry handler
],
}

# Instead, configure the root logger to send INFO-level logs to Sentry
logging.basicConfig(level=logging.INFO, handlers=[SentryLogsHandler(level=logging.INFO)])
```

### Loguru

Loguru logs can also be automatically captured and sent to Sentry.

```python
import sentry_sdk
from loguru import logger

sentry_sdk.init(
dsn="___PUBLIC_DSN___",
_experiments={
"enable_logs": True,
},
)

loguru.debug("In this example, debug events will not be sent to Sentry logs.")
loguru.info("On the other hand, info events will be sent to Sentry logs.")
```

By default, the Loguru integration sends `INFO`-level and higher logs to Sentry logs as long as `enable_logs` is `True`. A different threshold can be set via the `LoguruIntegration`'s `sentry_logs_level` parameter.

```python
import sentry_sdk
from loguru import logger
from sentry_sdk.integrations.loguru import LoggingLevels, LoguruIntegration

sentry_sdk.init(
dsn="___PUBLIC_DSN___",
_experiments={
"enable_logs": True
},
integrations=[
# Only send WARNING (and higher) logs to Sentry logs
LoguruIntegration(sentry_logs_level=LoggingLevels.WARNING.value),
],
)

logger.info("This INFO log won't be sent to Sentry logs.")
logger.warning("This WARNING log will be sent to Sentry logs.")
```

If you want other logging integrations to send logs to Sentry logs, but not Loguru, setting `sentry_logs_level` to `None` on the integration level will stop the Loguru integration for capturing Sentry logs.

```python

import sentry_sdk
from loguru import logger
from sentry_sdk.integrations.loguru import LoguruIntegration

sentry_sdk.init(
dsn="___PUBLIC_DSN___",
_experiments={
# In general, we want to capture logs as Sentry logs...
"enable_logs": True,
},
integrations=[
# ...just not from Loguru
LoguruIntegration(sentry_logs_level=None),
]
)

loguru.error("This won't be sent to Sentry logs.")
```