|
| 1 | +--- |
| 2 | +title: Set Up Logs in Rust |
| 3 | +sidebar_title: Logs |
| 4 | +description: "Structured logs allow you to send, view, and query logs sent from your applications within Sentry." |
| 5 | +sidebar_order: 5600 |
| 6 | +--- |
| 7 | + |
| 8 | +<Include name="feature-stage-beta-logs.mdx" /> |
| 9 | + |
| 10 | +With Sentry Structured Logs, you can send text based log information from your applications to Sentry. Once in Sentry, these logs can be viewed alongside relevant errors, searched by text-string, or searched using their individual attributes. |
| 11 | + |
| 12 | +## Requirements |
| 13 | + |
| 14 | +Logs in Rust are supported in Sentry Rust SDK version `0.39.0` and above. |
| 15 | +Additionally, the `logs` feature flag needs to be enabled. |
| 16 | + |
| 17 | +```toml {filename:Cargo.toml} |
| 18 | +[dependencies] |
| 19 | +sentry = { version = "{{@inject packages.version('sentry.rust') }}", features = ["logs"] } |
| 20 | +``` |
| 21 | + |
| 22 | +## Setup |
| 23 | + |
| 24 | +To enable logging, you need to initialize the SDK with the `enable_logs` option set to `true`. |
| 25 | + |
| 26 | +```rust |
| 27 | +let _guard = sentry::init(("___PUBLIC_DSN___", sentry::ClientOptions { |
| 28 | + release: sentry::release_name!(), |
| 29 | + enable_logs: true, |
| 30 | + ..Default::default() |
| 31 | +})); |
| 32 | +``` |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | +Once the feature is enabled on the SDK and the SDK is initialized, you can send logs by using the logging macros. |
| 37 | +The `sentry` crate exposes macros that support six different log levels: |
| 38 | +`logger_trace`, `logger_debug`, `logger_info`, `logger_warn`, `logger_error` and `logger_fatal`. |
| 39 | +The macros support logging a simple message, or a message with parameters, with `format` syntax: |
| 40 | + |
| 41 | +```rust |
| 42 | +use sentry::logger_info; |
| 43 | + |
| 44 | +logger_info!("Hello, world!"); |
| 45 | +logger_info!("Hello, {}!", "world"); |
| 46 | +``` |
| 47 | + |
| 48 | +You can also attach additional attributes to a log using the `key = value` syntax before the message: |
| 49 | + |
| 50 | +```rust |
| 51 | +use sentry::logger_error; |
| 52 | + |
| 53 | +logger_error!( |
| 54 | + database.host = "prod-db-01", |
| 55 | + database.port = 5432, |
| 56 | + database.name = "user_service", |
| 57 | + retry_attempt = 2, |
| 58 | + beta_features = false, |
| 59 | + "Database connection failed" |
| 60 | +); |
| 61 | +``` |
| 62 | + |
| 63 | +The supported attribute keys consist of any number of valid Rust identifiers, separated by dots. |
| 64 | +Attributes containing dots will be nested under their common prefix when displayed in the UI. |
| 65 | + |
| 66 | +The supported attribute values correspond to the values that can be converted to a `serde_json::Value`, |
| 67 | +which include primitive types for numbers, `bool`, and string types. |
| 68 | +As of today, array and object types will be converted to strings using their JSON representation. |
| 69 | + |
| 70 | +## Integrations |
| 71 | + |
| 72 | +We're actively working on adding integration support for Logs. |
| 73 | +Currently, we're looking at adding support for the `tracing` and `log` crates. |
| 74 | +You can follow progress on the following GitHub issues or open a [new one](https://github.com/getsentry/sentry-rust/issues/new/choose) for any additional integration you would like to see. |
| 75 | +- [`tracing`](https://github.com/getsentry/sentry-rust/issues/799) |
| 76 | +- [`log`](https://github.com/getsentry/sentry-rust/issues/818) |
| 77 | + |
| 78 | +## Options |
| 79 | + |
| 80 | +### `before_send_log` |
| 81 | + |
| 82 | +To filter logs, or update them before they are sent to Sentry, you can use the `before_send_log` client option. |
| 83 | + |
| 84 | +```rust |
| 85 | +let _guard = sentry::init(("___PUBLIC_DSN___", sentry::ClientOptions { |
| 86 | + release: sentry::release_name!(), |
| 87 | + enable_logs: true, |
| 88 | + before_send_log: Some(std::sync::Arc::new(|log| { |
| 89 | + // filter out all trace level logs |
| 90 | + if log.level == sentry::protocol::LogLevel::Trace { |
| 91 | + return None; |
| 92 | + } |
| 93 | + Some(log) |
| 94 | + })), |
| 95 | + ..Default::default() |
| 96 | +})); |
| 97 | +``` |
0 commit comments