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
Copy file name to clipboardExpand all lines: docs/platforms/python/integrations/loguru/index.mdx
+41-37Lines changed: 41 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: Loguru
3
3
description: Learn about using Sentry with Loguru.
4
4
---
5
5
6
-
The [Loguru](https://github.com/Delgan/loguru#readme) integration lets you capture log messages and send them to Sentry.
6
+
The [Loguru](https://github.com/Delgan/loguru#readme) integration lets you capture log messages and send them to Sentry. Depending on your settings, Loguru logs can be captured as Sentry logs, as error events, or as breadcrumbs, or a combination of those.
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
@@ -21,7 +21,7 @@ uv add "sentry-sdk[loguru]"
21
21
22
22
## Configure
23
23
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`.
24
+
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
25
26
26
```python
27
27
import sentry_sdk
@@ -42,47 +42,50 @@ from loguru import logger
42
42
43
43
defmain():
44
44
sentry_sdk.init(...) # same as above
45
-
logger.debug("I am ignored")
46
-
logger.error("There was an error!")
45
+
logger.info("Logging some info")
47
46
48
47
main()
49
48
```
50
49
51
-
This will capture the `error` level log entry and send it as an error to Sentry.
50
+
This will capture the `INFO` level log and send it to Sentry logs.
51
+
52
52
53
53
## Behavior
54
54
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:
55
+
As long as `enable_logs` is `True`, logs with a level of `INFO`and higher will be captured as Sentry logs. The threshold can be configured via the [`sentry_logs_level` option](#options) TODO LINK.
56
56
57
-
```python
58
-
from loguru import logger
57
+
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).
59
58
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
-
```
59
+
`INFO` and above logs will also be captured as breadcrumbs. Use the [`level` integration option](#options) to adjust the threshold.
65
60
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`.
71
-
72
-
Loguru log records can additionally also be captured as [Sentry logs](/platforms/python/logs/) as long as the `enable_logs` option is `True`.
61
+
The following snippet demonstrates the default behavior:
73
62
74
63
```python
75
64
import sentry_sdk
76
65
from loguru import logger
77
66
78
67
sentry_sdk.init(
79
-
#...
68
+
...,
80
69
enable_logs=True,
81
70
)
82
71
83
-
logger.info("I will be sent to Sentry logs")
72
+
# The following will be captured as Sentry logs:
73
+
logger.info("I'm an INFO log")
74
+
logger.error("I'm an ERROR log", extra={"bar": 43})
75
+
logger.exception("I'm an exception log")
76
+
77
+
# DEBUG-level logs won't be captured by default
78
+
logger.debug("I'm a DEBUG log")
84
79
```
85
80
81
+
- All of the above logs except for the `DEBUG` level message will be sent to Sentry as logs.
82
+
- An error event with the message `"I'm an exception log"` will be created..
83
+
-`"I'm an INFO log"` will be attached as a breadcrumb to that event.
84
+
-`bar` will end up in the `extra` attributes of that event.
85
+
-`"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.
86
+
- The debug message `"I'm a DEBUG log"` will not be captured by Sentry. See the [`sentry_logs_level` option](#option) to adjust which levels should be sent to Sentry as logs, and the [`level` option](#option) to do adjust the level for capturing breadcrumbs.
87
+
88
+
86
89
### Ignoring a logger
87
90
88
91
Loggers can be noisy. You can ignore a logger by calling `ignore_logger`.
@@ -100,17 +103,17 @@ In `a.spammy.logger` module:
100
103
101
104
```python
102
105
from loguru import logger
103
-
logger.error("hi") #No error is sent to Sentry
106
+
logger.error("hi") #Nothing is sent to Sentry
104
107
```
105
108
106
109
This will work with `logging`'s logger too
107
110
108
111
```python
109
112
logger = logging.getLogger("a.spammy.logger")
110
-
logger.error("hi") # Again, no error sent to Sentry
113
+
logger.error("hi") # Again, nothing is sent to Sentry
111
114
```
112
115
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.
116
+
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
117
115
118
## Options
116
119
@@ -127,25 +130,14 @@ sentry_sdk.init(
127
130
# ...
128
131
integrations=[
129
132
LoguruIntegration(
133
+
sentry_logs_level=LoggingLevels.INFO.value, # Capture INFO and above as logs
130
134
level=LoggingLevels.INFO.value, # Capture INFO and above as breadcrumbs
131
135
event_level=LoggingLevels.ERROR.value, # Send ERROR logs as events
132
-
sentry_logs_level=LoggingLevels.INFO.value, # Capture INFO and above as logs
133
136
)
134
137
],
135
138
)
136
139
```
137
140
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
141
150
142
-`sentry_logs_level`
151
143
@@ -162,6 +154,18 @@ sentry_sdk.init(
162
154
163
155
Default: `INFO`
164
156
157
+
-`level`
158
+
159
+
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.
160
+
161
+
Default: `INFO`
162
+
163
+
-`event_level`
164
+
165
+
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