Skip to content

Commit 9d6909f

Browse files
authored
feat(python): Add Loguru to Sentry logs docs (#14028)
- add basic Loguru setup examples to the [Structured logs page](https://sentry-docs-git-ivana-pythonloguru-sentry-logs.sentry.dev/platforms/python/logs/#loguru) - link to the logs docs page from the [Loguru integration page](https://sentry-docs-git-ivana-pythonloguru-sentry-logs.sentry.dev/platforms/python/integrations/loguru/) - link to the logs docs page from the [logging integration page](https://sentry-docs-git-ivana-pythonloguru-sentry-logs.sentry.dev/platforms/python/integrations/logging/)
1 parent 56be86d commit 9d6909f

File tree

3 files changed

+110
-14
lines changed

3 files changed

+110
-14
lines changed

docs/platforms/python/integrations/logging/index.mdx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ main()
5454
- `"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.
5555
- The debug message `"I am ignored"` will not surface anywhere. To capture it, you need to lower `level` to `DEBUG` (See below).
5656

57+
Log records can additionally also be captured as [Sentry logs](/platforms/python/logs/) as long as the `enable_logs` experimental option is `True`.
58+
59+
```python
60+
import logging
61+
import sentry_sdk
62+
63+
sentry_sdk.init(
64+
# ...
65+
_experiments={
66+
"enable_logs": True,
67+
},
68+
)
69+
70+
logging.info("I will be sent to Sentry logs")
71+
```
72+
5773
## Options
5874

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

87103
- `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.
88104

89-
- `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`:
105+
- `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`.
90106

91107
```python
92108
sentry_sdk.init(

docs/platforms/python/integrations/loguru/index.mdx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@ logger.exception("An exception happened")
6868
- `"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.
6969
- The debug message `"I am ignored"` will not be captured by Sentry. To capture it, set `level` to `DEBUG` or lower in `LoguruIntegration`.
7070

71+
Loguru log records can additionally also be captured as [Sentry logs](/platforms/python/logs/) as long as the `enable_logs` experimental option is `True`.
72+
73+
```python
74+
import sentry_sdk
75+
from loguru import logger
76+
77+
sentry_sdk.init(
78+
# ...
79+
_experiments={
80+
"enable_logs": True,
81+
},
82+
)
83+
84+
logger.info("I will be sent to Sentry logs")
85+
```
86+
7187
### Ignoring a logger
7288

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

135151
- `sentry_logs_level`
136152

137-
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.
153+
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.
138154

139155
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).
140156

platform-includes/logs/integrations/python.mdx

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
### Python Logger Integration
1+
### Standard library logging
22

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

55
```python
66
import sentry_sdk
@@ -9,8 +9,8 @@ import logging
99
sentry_sdk.init(
1010
dsn="___PUBLIC_DSN___",
1111
_experiments={
12-
"enable_logs": True
13-
}
12+
"enable_logs": True,
13+
},
1414
)
1515

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

25-
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.
25+
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.
2626

2727
```python
2828
import sentry_sdk
@@ -61,15 +61,79 @@ from sentry_sdk.integrations.logging import SentryLogsHandler
6161
from sentry_sdk.integrations.logging import LoggingIntegration
6262

6363
sentry_sdk.init(
64-
dsn="...",
65-
_experiments={
66-
"enable_logs": True
67-
},
68-
integrations=[
69-
LoggingIntegration(sentry_logs_level=None), # Do not monkeypatch the sentry handler
70-
]
64+
dsn="___PUBLIC_DSN___",
65+
_experiments={
66+
"enable_logs": True
67+
},
68+
integrations=[
69+
LoggingIntegration(sentry_logs_level=None), # Do not monkeypatch the sentry handler
70+
],
7171
}
7272

7373
# Instead, configure the root logger to send INFO-level logs to Sentry
7474
logging.basicConfig(level=logging.INFO, handlers=[SentryLogsHandler(level=logging.INFO)])
7575
```
76+
77+
### Loguru
78+
79+
Loguru logs can also be automatically captured and sent to Sentry.
80+
81+
```python
82+
import sentry_sdk
83+
from loguru import logger
84+
85+
sentry_sdk.init(
86+
dsn="___PUBLIC_DSN___",
87+
_experiments={
88+
"enable_logs": True,
89+
},
90+
)
91+
92+
loguru.debug("In this example, debug events will not be sent to Sentry logs.")
93+
loguru.info("On the other hand, info events will be sent to Sentry logs.")
94+
```
95+
96+
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.
97+
98+
```python
99+
import sentry_sdk
100+
from loguru import logger
101+
from sentry_sdk.integrations.loguru import LoggingLevels, LoguruIntegration
102+
103+
sentry_sdk.init(
104+
dsn="___PUBLIC_DSN___",
105+
_experiments={
106+
"enable_logs": True
107+
},
108+
integrations=[
109+
# Only send WARNING (and higher) logs to Sentry logs
110+
LoguruIntegration(sentry_logs_level=LoggingLevels.WARNING.value),
111+
],
112+
)
113+
114+
logger.info("This INFO log won't be sent to Sentry logs.")
115+
logger.warning("This WARNING log will be sent to Sentry logs.")
116+
```
117+
118+
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.
119+
120+
```python
121+
122+
import sentry_sdk
123+
from loguru import logger
124+
from sentry_sdk.integrations.loguru import LoguruIntegration
125+
126+
sentry_sdk.init(
127+
dsn="___PUBLIC_DSN___",
128+
_experiments={
129+
# In general, we want to capture logs as Sentry logs...
130+
"enable_logs": True,
131+
},
132+
integrations=[
133+
# ...just not from Loguru
134+
LoguruIntegration(sentry_logs_level=None),
135+
]
136+
)
137+
138+
loguru.error("This won't be sent to Sentry logs.")
139+
```

0 commit comments

Comments
 (0)