Skip to content

Commit e882001

Browse files
authored
fix(python): fix Python logs docs (#13685)
1 parent 0238af5 commit e882001

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

platform-includes/logs/integrations/python.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
### Python Logger Integration
22

3-
Sentry automatically instruments Python loggers via it's `LoggingIntegration`.
3+
Sentry automatically instruments Python loggers via its `LoggingIntegration`.
44

55
```python
6+
import sentry_sdk
67
import logging
78

89
sentry_sdk.init(
@@ -15,6 +16,7 @@ sentry_sdk.init(
1516
# Your existing logging setup
1617
my_logger = logging.Logger("some-logger")
1718

19+
my_value = 42
1820
my_logger.debug('In this example debug events will not be sent to Sentry logs. my_value=%s', my_value)
1921
my_logger.info('But info events will be sent to Sentry logs. my_value=%s', my_value)
2022
```
Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
#### before_send_log
22

3-
To filter logs, or update them before they are sent to Sentry, you can use the `_experiments.before_send_log` option.
3+
To filter logs, or update them before they are sent to Sentry, you can use the `_experiments["before_send_log"]` option.
44

55
```python
6-
def _before_log(log):
7-
if log.level == "info":
8-
# Filter out all info logs
9-
return None
6+
import sentry_sdk
7+
from sentry_sdk.types import Log, Hint
8+
from typing import Optional
109

10+
def before_log(log: Log, _hint: Hint) -> Optional[Log]:
11+
# Filter out all info level logs
12+
if log["severity_text"] == "info":
13+
return None
1114
return log
1215

1316
sentry_sdk.init(
1417
dsn="___PUBLIC_DSN___",
1518
_experiments={
1619
"enable_logs": True,
17-
"before_send_log": _before_log,
20+
"before_send_log": before_log,
1821
},
1922
)
2023
```
2124

2225
The `before_send_log` function receives a log object, and should return the log object if you want it to be sent to Sentry, or `None` if you want to discard it.
2326

24-
The log object has the following properties:
27+
The log dict has the following keys:
2528

26-
- `level`: (string - one of `trace`, `debug`, `info`, `warn`, `error`, `fatal`) The log level.
27-
- `message`: (string) The message to be logged.
28-
- `timestamp`: (number) The timestamp of the log.
29-
- `attributes`: (object) The attributes of the log.
29+
- `severity_text`: (`str` - one of `trace`, `debug`, `info`, `warning`, `error`, `fatal`) The log level.
30+
- `severity_number`: (`int`) The log level as a number ranging from 1 to 24, as per the OpenTelemetry specification of [`SeverityNumber`](https://opentelemetry.io/docs/specs/otel/logs/data-model/#field-severitynumber).
31+
- `body`: (`str`) The log message.
32+
- `attributes`: (`dict[str, str | bool | float | int]`) Additional attributes to be sent with the log.
33+
- `time_unix_nano`: (`int`) The timestamp of the log in nanoseconds since the Unix epoch.
34+
- `trace_id`: (`Optional[str]`) The trace ID of the trace this log belongs to.
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
Logs for Python are supported in Sentry Python SDK version `2.27.0` and above.
22

3-
```bash
4-
pip install --upgrade sentry-sdk
3+
```bash {tabTitle:pip}
4+
pip install "sentry-sdk"
55
```
6+
```bash {tabTitle:uv}
7+
uv add "sentry-sdk"
8+
```

platform-includes/logs/setup/python.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
To enable logging, you need to initialize the SDK with the `_experiments.enable_logs` option set to `true`.
1+
To enable logging, you need to initialize the SDK with the `_experiments["enable_logs"]` option set to `True`.
22

33
```python
44
sentry_sdk.init(
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the `sentry_sdk.logger` APIs.
22

3-
The `logger` namespace exposes six methods that you can use to log messages at different log levels: `trace`, `debug`, `info`, `warn`, `error`, and `fatal`.
3+
The `logger` namespace exposes six methods that you can use to log messages at different log levels: `trace`, `debug`, `info`, `warning`, `error`, and `fatal`.
44

55
You can pass additional attributes directly to the logging functions. These properties will be sent to Sentry, and can be searched from within the Logs UI, and even added to the Logs views as a dedicated column.
66

77
```python
88
from sentry_sdk import logger as sentry_logger
99

10-
sentry_logger.trace('Starting database connection {database}', database="users");
11-
sentry_logger.debug('Cache miss for user {user_id}', user_id=123);
12-
sentry_logger.info('Updated global cache');
13-
sentry_logger.warn('Rate limit reached for endpoint {endpoint}', endpoint='/api/results/');
14-
sentry_logger.error('Failed to process payment. Order: {order_id}. Amount: {amount}', order_id="or_2342", amount=99.99);
15-
sentry_logger.fatal('Database {database} connection pool exhausted', database="users");
10+
sentry_logger.trace('Starting database connection {database}', database="users")
11+
sentry_logger.debug('Cache miss for user {user_id}', user_id=123)
12+
sentry_logger.info('Updated global cache')
13+
sentry_logger.warning('Rate limit reached for endpoint {endpoint}', endpoint='/api/results/')
14+
sentry_logger.error('Failed to process payment. Order: {order_id}. Amount: {amount}', order_id="or_2342", amount=99.99)
15+
sentry_logger.fatal('Database {database} connection pool exhausted', database="users")
1616
```

0 commit comments

Comments
 (0)