Skip to content

Commit 0819405

Browse files
lciancoolguyzone
authored andcommitted
feat(rust): document tracing and log integration for logs (#14045)
Co-authored-by: Alex Krawiec <alex.krawiec@sentry.io>
1 parent b40ecae commit 0819405

File tree

1 file changed

+125
-10
lines changed

1 file changed

+125
-10
lines changed

docs/platforms/rust/common/logs/index.mdx

Lines changed: 125 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ sentry = { version = "{{@inject packages.version('sentry.rust') }}", features =
2424
To enable logging, you need to initialize the SDK with the `enable_logs` option set to `true`.
2525

2626
```rust
27-
let _guard = sentry::init(("___PUBLIC_DSN___", sentry::ClientOptions {
28-
release: sentry::release_name!(),
29-
enable_logs: true,
30-
..Default::default()
31-
}));
27+
let _guard = sentry::init((
28+
"___PUBLIC_DSN___",
29+
sentry::ClientOptions {
30+
release: sentry::release_name!(),
31+
enable_logs: true,
32+
..Default::default()
33+
}
34+
));
3235
```
3336

3437
## Usage
@@ -69,11 +72,123 @@ As of today, array and object types will be converted to strings using their JSO
6972

7073
## Integrations
7174

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)
75+
### `tracing` Integration
76+
77+
To use the `tracing` integration with logs, add the necessary dependencies to your `Cargo.toml`.
78+
79+
```toml {filename:Cargo.toml}
80+
[dependencies]
81+
sentry = { version = "{{@inject packages.version('sentry.rust') }}", features = ["tracing", "logs"] }
82+
tracing = "0.1.41"
83+
tracing-subscriber = "0.3.19"
84+
```
85+
86+
Initialize the SDK with `enable_logs` set to `true`, and configure the `tracing` subscriber to map `tracing` events to logs based on metadata such as severity.
87+
Then, use standard `tracing` macros to capture logs.
88+
89+
```rust {filename:main.rs}
90+
use sentry::integrations::tracing::EventFilter;
91+
use tracing::{error, info, warn};
92+
use tracing_subscriber::prelude::*;
93+
94+
fn main() {
95+
let _guard = sentry::init((
96+
"___PUBLIC_DSN___",
97+
sentry::ClientOptions {
98+
release: sentry::release_name!(),
99+
enable_logs: true,
100+
..Default::default()
101+
},
102+
));
103+
104+
let sentry_layer =
105+
sentry::integrations::tracing::layer().event_filter(|md| match *md.level() {
106+
// Capture error events as Sentry events
107+
// These are grouped into issues, representing high-severity errors to act upon
108+
tracing::Level::ERROR => EventFilter::Event,
109+
// Ignore trace level events, as they're too verbose
110+
tracing::Level::TRACE => EventFilter::Ignore,
111+
// Capture everything else as a traditional structured log
112+
_ => EventFilter::Log,
113+
});
114+
115+
tracing_subscriber::registry()
116+
.with(tracing_subscriber::fmt::layer())
117+
.with(sentry_layer)
118+
.init();
119+
120+
info!("Hello, world!");
121+
122+
warn!(
123+
database.host = "prod-db-01",
124+
database.port = 5432,
125+
retry_attempt = 2,
126+
"Database connection slow"
127+
);
128+
129+
error!(error_code = 500, "Critical system failure");
130+
}
131+
```
132+
133+
The fields of `tracing` events are automatically captured as attributes in Sentry logs.
134+
135+
To map a `tracing` event to multiple `EventFilter`s, for example to capture both an event and a log for error level `tracing` events, use an additional Sentry layer with the desired event filter.
136+
137+
### `log` Integration
138+
139+
To use the `log` integration with logs, add the necessary dependencies to your `Cargo.toml`.
140+
141+
```toml {filename:Cargo.toml}
142+
[dependencies]
143+
sentry = { version = "{{@inject packages.version('sentry.rust') }}", features = ["log", "logs"] }
144+
log = "0.4"
145+
# optional: use any log implementation you prefer
146+
env_logger = "0.11"
147+
```
148+
149+
Initialize the SDK with `enable_logs` set to `true`, and configure the `log` integration to map `log` records to logs based on metadata such as severity.
150+
Then, use standard `log` macros to capture logs.
151+
152+
```rust {filename:main.rs}
153+
use sentry_log::LogFilter;
154+
use log::{info, warn, error};
155+
156+
fn main() {
157+
let _guard = sentry::init((
158+
"___PUBLIC_DSN___",
159+
sentry::ClientOptions {
160+
release: sentry::release_name!(),
161+
enable_logs: true,
162+
..Default::default()
163+
},
164+
));
165+
166+
let logger = sentry::integrations::log::SentryLogger::with_dest(
167+
env_logger::Builder::from_default_env().build(),
168+
)
169+
.filter(|md| match md.level() {
170+
// Capture error records as Sentry events
171+
// These are grouped into issues, representing high-severity errors to act upon
172+
log::Level::Error => LogFilter::Event,
173+
// Ignore trace level records, as they're too verbose
174+
log::Level::Trace => LogFilter::Ignore,
175+
// Capture everything else as a log
176+
_ => LogFilter::Log,
177+
});
178+
179+
log::set_boxed_logger(Box::new(logger)).unwrap();
180+
log::set_max_level(log::LevelFilter::Trace);
181+
182+
info!("Hello, world!");
183+
warn!("Database connection slow");
184+
error!("Critical system failure");
185+
}
186+
```
187+
188+
### Additional Integrations
189+
190+
We're always looking to expand integration support for Logs.
191+
If you'd like to see support for additional logging libraries, please open a [new issue](https://github.com/getsentry/sentry-rust/issues/new/choose) with your request.
77192

78193
## Options
79194

0 commit comments

Comments
 (0)