You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]>
Copy file name to clipboardExpand all lines: docs/platforms/python/integrations/loguru/index.mdx
+48-36Lines changed: 48 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,13 @@ The [Loguru](https://github.com/Delgan/loguru#readme) integration lets you captu
7
7
8
8
The [`logging`](/platforms/python/integrations/logging) integration provides most of the Loguru functionality and most examples on that page work with Loguru.
9
9
10
+
<Alertlevel="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
+
10
17
## Install
11
18
12
19
Install `sentry-sdk` from PyPI with the `loguru` extra.
@@ -21,7 +28,7 @@ uv add "sentry-sdk[loguru]"
21
28
22
29
## Configure
23
30
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.
25
32
26
33
```python
27
34
import sentry_sdk
@@ -42,47 +49,51 @@ from loguru import logger
42
49
43
50
defmain():
44
51
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")
47
54
48
55
main()
49
56
```
50
57
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.
52
59
53
-
## Behavior
54
60
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
56
62
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).
59
64
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).
65
66
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.
71
68
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:
73
70
74
71
```python
75
72
import sentry_sdk
76
73
from loguru import logger
77
74
78
75
sentry_sdk.init(
79
-
#...
76
+
...,
80
77
enable_logs=True,
81
78
)
82
79
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")
84
87
```
85
88
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
+
86
97
### Ignoring a logger
87
98
88
99
Loggers can be noisy. You can ignore a logger by calling `ignore_logger`.
@@ -100,17 +111,17 @@ In `a.spammy.logger` module:
100
111
101
112
```python
102
113
from loguru import logger
103
-
logger.error("hi") #No error is sent to Sentry
114
+
logger.error("hi") #Nothing is sent to Sentry
104
115
```
105
116
106
117
This will work with `logging`'s logger too
107
118
108
119
```python
109
120
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
111
122
```
112
123
113
-
You can also use `before-send`and `before-breadcrumb` to ignore only certain messages. See <PlatformLinkto="/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 <PlatformLinkto="/configuration/filtering/">Filtering Events</PlatformLink> for more information.
114
125
115
126
## Options
116
127
@@ -127,25 +138,14 @@ sentry_sdk.init(
127
138
# ...
128
139
integrations=[
129
140
LoguruIntegration(
141
+
sentry_logs_level=LoggingLevels.INFO.value, # Capture INFO and above as logs
130
142
level=LoggingLevels.INFO.value, # Capture INFO and above as breadcrumbs
131
143
event_level=LoggingLevels.ERROR.value, # Send ERROR logs as events
132
-
sentry_logs_level=LoggingLevels.INFO.value, # Capture INFO and above as logs
133
144
)
134
145
],
135
146
)
136
147
```
137
148
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`
149
149
150
150
-`sentry_logs_level`
151
151
@@ -162,6 +162,18 @@ sentry_sdk.init(
162
162
163
163
Default: `INFO`
164
164
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.
0 commit comments