Skip to content

Commit 04b78ba

Browse files
ref(loguru): Integrate Sentry logs into Loguru integration page better (#15531)
Update Loguru integration docs: - make it clearer what the integration actually does (since it currently serves three different features) - make it clearer how to configure each - emphasize the Sentry Logs part as that is the main feature **Direct link to preview:** https://sentry-docs-git-ivana-pythonlogging-docs-revamp.sentry.dev/platforms/python/integrations/loguru/ Ref getsentry/sentry-python#5090 ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. - [ ] Urgent deadline (GA date, etc.): <!-- ENTER DATE HERE --> - [ ] Other deadline: <!-- ENTER DATE HERE --> - [x] None: Not urgent, can wait up to 1 week+ ## SLA - Teamwork makes the dream work, so please add a reviewer to your PRs. - Please give the docs team up to 1 week to review your PR unless you've added an urgent due date to it. Thanks in advance for your help! ## PRE-MERGE CHECKLIST *Make sure you've checked the following before merging your changes:* - [x] Checked Vercel preview for correctness, including links - [ ] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [ ] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs) ## LEGAL BOILERPLATE <!-- Sentry employees and contractors can delete or ignore this section. --> Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms. ## EXTRA RESOURCES - [Sentry Docs contributor guide](https://docs.sentry.io/contributing/) --------- Co-authored-by: Alex Alderman Webb <[email protected]>
1 parent 5819cdd commit 04b78ba

File tree

1 file changed

+48
-36
lines changed
  • docs/platforms/python/integrations/loguru

1 file changed

+48
-36
lines changed

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

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ The [Loguru](https://github.com/Delgan/loguru#readme) integration lets you captu
77

88
The [`logging`](/platforms/python/integrations/logging) integration provides most of the Loguru functionality and most examples on that page work with Loguru.
99

10+
<Alert level="info" title="Sentry Logs for Loguru">
11+
12+
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.
13+
14+
</Alert>
15+
16+
1017
## Install
1118

1219
Install `sentry-sdk` from PyPI with the `loguru` extra.
@@ -21,7 +28,7 @@ uv add "sentry-sdk[loguru]"
2128

2229
## Configure
2330

24-
If you have the `loguru` package in your dependencies, the Loguru integration will be enabled automatically when you initialize the Sentry SDK. To capture Loguru log records as [Sentry logs](/platforms/python/logs/), set `enable_logs` to `True`.
31+
To capture Loguru log records as [Sentry logs](/platforms/python/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.
2532

2633
```python
2734
import sentry_sdk
@@ -42,47 +49,51 @@ from loguru import logger
4249

4350
def main():
4451
sentry_sdk.init(...) # same as above
45-
logger.debug("I am ignored")
46-
logger.error("There was an error!")
52+
logger.info("Logging some info")
53+
logger.error("Logging an error")
4754

4855
main()
4956
```
5057

51-
This will capture the `error` level log entry and send it as an error to Sentry.
58+
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.
5259

53-
## Behavior
5460

55-
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:
61+
## Behavior
5662

57-
```python
58-
from loguru import logger
63+
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](#options).
5964

60-
logger.debug("I am ignored")
61-
logger.info("I am a breadcrumb")
62-
logger.error("I am an event", extra=dict(bar=43))
63-
logger.exception("An exception happened")
64-
```
65+
Additionally, the Loguru integration will create an error event from all `ERROR`-level logs. This feature is configurable via the [`event_level` integration option](#options).
6566

66-
- An error event with the message `"I am an event"` will be created.
67-
- `"I am a breadcrumb"` will be attached as a breadcrumb to that event.
68-
- `bar` will end up in the `extra` attributes of that event.
69-
- `"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.
70-
- The debug message `"I am ignored"` will not be captured by Sentry. To capture it, set `level` to `DEBUG` or lower in `LoguruIntegration`.
67+
`INFO` and above logs will also be captured as breadcrumbs. Use the [`level` integration option](#options) to adjust the threshold.
7168

72-
Loguru log records can additionally also be captured as [Sentry logs](/platforms/python/logs/) as long as the `enable_logs` option is `True`.
69+
The following snippet demonstrates the default behavior:
7370

7471
```python
7572
import sentry_sdk
7673
from loguru import logger
7774

7875
sentry_sdk.init(
79-
# ...
76+
...,
8077
enable_logs=True,
8178
)
8279

83-
logger.info("I will be sent to Sentry logs")
80+
# The following will be captured as Sentry logs:
81+
logger.info("I'm an INFO log")
82+
logger.error("I'm an ERROR log", extra={"bar": 43})
83+
logger.exception("I'm an exception log")
84+
85+
# DEBUG-level logs won't be captured by default
86+
logger.debug("I'm a DEBUG log")
8487
```
8588

89+
- All of the above logs except for the `DEBUG`-level message will be sent to Sentry as logs.
90+
- An error event with the message `"I'm an ERROR log"` will be created.
91+
- `"I'm an INFO log"` will be attached as a breadcrumb to that event.
92+
- `bar` will end up in the `extra` attributes of that event.
93+
- `"I'm an exception log"` 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.
94+
- The debug message `"I'm a DEBUG log"` will not be captured by Sentry. See the [`sentry_logs_level` option](#option) to adjust which log levels should be sent to Sentry as logs, and the [`level` option](#option) to adjust the level for capturing breadcrumbs.
95+
96+
8697
### Ignoring a logger
8798

8899
Loggers can be noisy. You can ignore a logger by calling `ignore_logger`.
@@ -100,17 +111,17 @@ In `a.spammy.logger` module:
100111

101112
```python
102113
from loguru import logger
103-
logger.error("hi") # No error is sent to Sentry
114+
logger.error("hi") # Nothing is sent to Sentry
104115
```
105116

106117
This will work with `logging`'s logger too
107118

108119
```python
109120
logger = logging.getLogger("a.spammy.logger")
110-
logger.error("hi") # Again, no error sent to Sentry
121+
logger.error("hi") # Again, nothing is sent to Sentry
111122
```
112123

113-
You can also use `before-send` and `before-breadcrumb` to ignore only certain messages. See <PlatformLink to="/configuration/filtering/">Filtering Events</PlatformLink> for more information.
124+
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 <PlatformLink to="/configuration/filtering/">Filtering Events</PlatformLink> for more information.
114125

115126
## Options
116127

@@ -127,25 +138,14 @@ sentry_sdk.init(
127138
# ...
128139
integrations=[
129140
LoguruIntegration(
141+
sentry_logs_level=LoggingLevels.INFO.value, # Capture INFO and above as logs
130142
level=LoggingLevels.INFO.value, # Capture INFO and above as breadcrumbs
131143
event_level=LoggingLevels.ERROR.value, # Send ERROR logs as events
132-
sentry_logs_level=LoggingLevels.INFO.value, # Capture INFO and above as logs
133144
)
134145
],
135146
)
136147
```
137148

138-
- `level`
139-
140-
The Sentry Python SDK will record log records with a level higher than or equal to `level` as 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.
141-
142-
Default: `INFO`
143-
144-
- `event_level`
145-
146-
The Sentry Python SDK will report log records with a level higher than or equal to `event_level` as events. If set to `None`, the SDK won't send log records as events.
147-
148-
Default: `ERROR`
149149

150150
- `sentry_logs_level`
151151

@@ -162,6 +162,18 @@ sentry_sdk.init(
162162

163163
Default: `INFO`
164164

165+
- `level`
166+
167+
The Sentry Python SDK will record log records with a level higher than or equal to `level` as breadcrumbs. Inversely, the SDK will not capture breadcrumbs for logs with a level lower than this one. If set to `None`, the SDK won't send log records as breadcrumbs.
168+
169+
Default: `INFO`
170+
171+
- `event_level`
172+
173+
The Sentry Python SDK will report log records with a level higher than or equal to `event_level` as events. If set to `None`, the SDK won't send log records as events.
174+
175+
Default: `ERROR`
176+
165177
## Supported Versions
166178

167179
- Loguru: 0.5+

0 commit comments

Comments
 (0)