|
1 | 1 | #### before_send_log |
2 | 2 |
|
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. |
4 | 4 |
|
5 | 5 | ```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 |
10 | 9 |
|
| 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 |
11 | 14 | return log |
12 | 15 |
|
13 | 16 | sentry_sdk.init( |
14 | 17 | dsn="___PUBLIC_DSN___", |
15 | 18 | _experiments={ |
16 | 19 | "enable_logs": True, |
17 | | - "before_send_log": _before_log, |
| 20 | + "before_send_log": before_log, |
18 | 21 | }, |
19 | 22 | ) |
20 | 23 | ``` |
21 | 24 |
|
22 | 25 | 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. |
23 | 26 |
|
24 | | -The log object has the following properties: |
| 27 | +The log dict has the following keys: |
25 | 28 |
|
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. |
0 commit comments