Skip to content

Commit ed0daa6

Browse files
authored
ref(logging): Update logging integration docs to better integrate Sentry logs (#15533)
The Python stdlib logging integration docs page still emphasizes breadcrumbs and creating error events, and logs are sidelined even though they're the main feature. Updating to reflect this. **Direct link to preview:** https://sentry-docs-git-ivana-pythonstdlib-logging-docs-revamp.sentry.dev/platforms/python/integrations/logging/ 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/)
1 parent ff083a3 commit ed0daa6

File tree

1 file changed

+42
-20
lines changed
  • docs/platforms/python/integrations/logging

1 file changed

+42
-20
lines changed

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

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ title: Logging
33
description: "Learn about logging with Python."
44
---
55

6-
Adds support for Python logging.
6+
The logging integration adds support for the `logging` framework from Python's standard library. Depending on your settings, logs can be captured as Sentry logs, as error events, or as breadcrumbs, or a combination of those.
7+
8+
<Alert level="info" title="Sentry Logs">
9+
10+
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.
11+
12+
</Alert>
713

814
## Install
915

@@ -19,7 +25,7 @@ uv add "sentry-sdk"
1925

2026
## Configure
2127

22-
The logging integrations is a default integration so it will be enabled automatically when you initialize the Sentry SDK.
28+
To capture log records as [Sentry logs](/platforms/python/logs/), set `enable_logs` to `True`. The logging integration is a default integration, so it will be enabled automatically when you initialize the Sentry SDK.
2329

2430
```python
2531
import sentry_sdk
@@ -29,6 +35,7 @@ sentry_sdk.init(
2935
# Add data like request headers and IP for users, if applicable;
3036
# see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
3137
send_default_pii=True,
38+
enable_logs=True,
3239
)
3340
```
3441

@@ -39,35 +46,49 @@ import logging
3946

4047
def main():
4148
sentry_sdk.init(...) # same as above
42-
43-
logging.debug("I am ignored")
44-
logging.info("I am a breadcrumb")
45-
logging.error("I am an event", extra=dict(bar=43))
46-
logging.exception("An exception happened")
49+
logging.info("Logging some info")
50+
logging.error("Logging an error")
4751

4852
main()
4953
```
5054

51-
- There will be an error event with the message `"I am an event"`.
52-
- `"I am a breadcrumb"` will be attached as a breadcrumb to that event.
53-
- `bar` will end up in the event's `extra` attributes.
54-
- `"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.
55-
- The debug message `"I am ignored"` will not surface anywhere. To capture it, you need to lower `level` to `DEBUG` (See below).
55+
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.
56+
57+
## Behavior
5658

57-
Log records can additionally also be captured as [Sentry logs](/platforms/python/logs/) as long as the `enable_logs` option is `True`.
59+
As long as `enable_logs` is `True`, logs with a level of `INFO` and higher will be captured as Sentry logs if the log level set in the `logging` module is `INFO` or below. The threshold can be configured via the [`sentry_logs_level` option](#options).
60+
61+
Additionally, the logging integration will create an error event from all `ERROR`-level logs. This feature is configurable via the [`event_level` integration option](#options).
62+
63+
`INFO` and above logs will also be captured as breadcrumbs. Use the [`level` integration option](#options) to adjust the threshold.
5864

5965
```python
6066
import logging
67+
6168
import sentry_sdk
6269

6370
sentry_sdk.init(
64-
# ...
71+
...,
6572
enable_logs=True,
6673
)
6774

68-
logging.info("I will be sent to Sentry logs")
75+
# The following will be captured as Sentry logs:
76+
logging.info("I'm an INFO log")
77+
logging.error("I'm an ERROR log", extra={"bar": 43})
78+
logging.exception("I'm an exception log")
79+
80+
# DEBUG-level logs won't be captured by default
81+
logging.debug("I'm a DEBUG log")
6982
```
7083

84+
- All of the above logs except for the `DEBUG` level message will be sent to Sentry as logs.
85+
- An error event with the message `"I'm an ERROR log"` will be created.
86+
- `"I'm an INFO log"` will be attached as a breadcrumb to that event.
87+
- `bar` will end up in the `extra` attributes of that event.
88+
- `"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.
89+
- 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.
90+
91+
7192
### Working with Extra Fields
7293

7394
When sending log records as Sentry logs, any fields provided in the `extra` dictionary are automatically promoted to top-level attributes on the log entry. This makes them searchable and filterable in the Sentry UI.
@@ -115,20 +136,16 @@ sentry_sdk.init(
115136
# ...
116137
integrations=[
117138
LoggingIntegration(
139+
sentry_logs_level=logging.INFO, # Capture INFO and above as logs
118140
level=logging.INFO, # Capture INFO and above as breadcrumbs
119141
event_level=logging.ERROR, # Send ERROR records as events
120-
sentry_logs_level=logging.INFO, # Capture INFO and above as logs
121142
),
122143
],
123144
)
124145
```
125146

126147
You can pass the following keyword arguments to `LoggingIntegration()`:
127148

128-
- `level` (default `INFO`): The Sentry Python SDK will record log records with a level higher than or equal to `level` as breadcrumbs. Inversely, the SDK completely ignores any log record with a level lower than this one. If a value of `None` occurs, the SDK won't send log records as breadcrumbs.
129-
130-
- `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.
131-
132149
- `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` option is `True`.
133150

134151
```python
@@ -138,6 +155,11 @@ You can pass the following keyword arguments to `LoggingIntegration()`:
138155
)
139156
```
140157

158+
- `level` (default `INFO`): The Sentry Python SDK will record log records with a level higher than or equal to `level` as breadcrumbs. Inversely, the SDK completely ignores any log record with a level lower than this one. If a value of `None` occurs, the SDK won't send log records as breadcrumbs.
159+
160+
- `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.
161+
162+
141163
<Alert title="Note">
142164

143165
The Sentry Python SDK will honor the configured level of each logger (set with `logger.setLevel(level)` or `logging.basicConfig(level=level)`). That means that you will not see any `INFO` or `DEBUG` events from a logger with the level set to `WARNING`, regardless of how you configure the integration. If not set explicitly, the logging level defaults to `WARNING`.

0 commit comments

Comments
 (0)