Skip to content

Commit 454a04f

Browse files
authored
ref(rust): clarify logs defaults in new release (#15036)
1 parent e9d3272 commit 454a04f

File tree

2 files changed

+27
-52
lines changed

2 files changed

+27
-52
lines changed

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

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,13 @@ sentry = { version = "{{@inject packages.version('sentry.rust') }}", features =
1919

2020
## Setup
2121

22-
To enable logging, you need to initialize the SDK with the `enable_logs` option set to `true`.
23-
24-
```rust
25-
let _guard = sentry::init((
26-
"___PUBLIC_DSN___",
27-
sentry::ClientOptions {
28-
release: sentry::release_name!(),
29-
enable_logs: true,
30-
..Default::default()
31-
}
32-
));
33-
```
22+
To enable logging, just add the `sentry` dependency with the `logs` feature flag.
23+
This will set `enable_logs: true` by default in your `sentry::ClientOptions`.
24+
You can opt-out from sending logs even while using the `logs` feature flag by explicitly initializing the SDK with `enable_logs: false`.
3425

3526
## Usage
3627

37-
Once the feature is enabled on the SDK and the SDK is initialized, you can send logs by using the logging macros.
28+
Once the feature is enabled and the SDK is initialized, you can send logs by using the logging macros.
3829
The `sentry` crate exposes macros that support six different log levels:
3930
`logger_trace`, `logger_debug`, `logger_info`, `logger_warn`, `logger_error` and `logger_fatal`.
4031
The macros support logging a simple message, or a message with parameters, with `format` syntax:
@@ -81,7 +72,6 @@ tracing = "0.1.41"
8172
tracing-subscriber = "0.3.19"
8273
```
8374

84-
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.
8575
Then, use standard `tracing` macros to capture logs.
8676

8777
```rust {filename:main.rs}
@@ -99,20 +89,9 @@ fn main() {
9989
},
10090
));
10191

102-
let sentry_layer =
103-
sentry::integrations::tracing::layer().event_filter(|md| match *md.level() {
104-
// Capture error level events as Sentry events
105-
// These are grouped into issues, representing high-severity errors to act upon
106-
tracing::Level::ERROR => EventFilter::Event,
107-
// Ignore trace level events, as they're too verbose
108-
tracing::Level::TRACE => EventFilter::Ignore,
109-
// Capture everything else as a traditional structured log
110-
_ => EventFilter::Log,
111-
});
112-
11392
tracing_subscriber::registry()
11493
.with(tracing_subscriber::fmt::layer())
115-
.with(sentry_layer)
94+
.with(sentry::integrations::tracing::layer())
11695
.init();
11796

11897
info!("Hello, world!");
@@ -130,19 +109,22 @@ fn main() {
130109

131110
The fields of `tracing` events are automatically captured as attributes in Sentry logs.
132111

133-
To map a `tracing` event to multiple items in Sentry (e.g. both an event and a log), combine multiple event filters using the bitwise or operator.
112+
By default, `tracing` events at or above info level are captured as Sentry logs.
113+
114+
This behavior can be customized by applying a custom `event_filter` when creating the layer.
115+
The following snippet shows the default `event_filter` that's applied when using the `sentry` crate with the `logs` feature flag.
134116

135117
```rust
136118
use sentry::integrations::tracing::EventFilter;
137119

138120
let sentry_layer =
139121
sentry::integrations::tracing::layer().event_filter(|md| match *md.level() {
140122
// Capture error and warn level events as both logs and events in Sentry
141-
tracing::Level::ERROR | tracing::Level::WARN => EventFilter::Event | EventFilter::Log,
123+
tracing::Level::ERROR => EventFilter::Event | EventFilter::Log,
142124
// Ignore trace level events, as they're too verbose
143125
tracing::Level::TRACE => EventFilter::Ignore,
144-
// Capture everything else just as a log
145-
_ => EventFilter::Log,
126+
// Capture everything else as both a breadcrumb and a log
127+
_ => EventFilter::Breadcrumb | EventFilter::Log,
146128
});
147129
```
148130

@@ -158,7 +140,6 @@ log = "0.4"
158140
env_logger = "0.11"
159141
```
160142

161-
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.
162143
Then, use standard `log` macros to capture logs.
163144

164145
```rust {filename:main.rs}
@@ -177,17 +158,7 @@ fn main() {
177158

178159
let logger = sentry::integrations::log::SentryLogger::with_dest(
179160
env_logger::Builder::from_default_env().build(),
180-
)
181-
.filter(|md| match md.level() {
182-
// Capture error records as Sentry events
183-
// These are grouped into issues, representing high-severity errors to act upon
184-
log::Level::Error => LogFilter::Event,
185-
// Ignore trace level records, as they're too verbose
186-
log::Level::Trace => LogFilter::Ignore,
187-
// Capture everything else as a log
188-
_ => LogFilter::Log,
189-
});
190-
161+
);
191162
log::set_boxed_logger(Box::new(logger)).unwrap();
192163
log::set_max_level(log::LevelFilter::Trace);
193164

docs/platforms/rust/guides/tracing/index.mdx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,25 @@ async fn fail() {
8888
}
8989
```
9090

91-
By default, error level events from `tokio-rs/tracing` are captured as Sentry events, while anything at or above info is added as a breadcrumb.
91+
By default, error-level events from `tracing` are captured as Sentry events, while events at or above info level are added to the current scope as breadcrumbs.
9292

93-
To capture structured logs from `tokio-rs/tracing` events instead, you need to pass `enable_logs: true` to `sentry::init`, and set up the Sentry layer with a custom event filter that maps to logs, like so:
93+
Additionally, if the `logs` feature flag of the `sentry` crate is enabled and the SDK is initialized with `enable_logs: true`, then events from `tracing` at info level or above are also captured as [structured logs](https://docs.sentry.io/product/explore/logs/).
94+
95+
This behavior can be customized by applying a custom `event_filter` when creating the layer.
96+
The following snippet shows the default `event_filter` that's applied when using the `sentry` crate with the `logs` feature flag.
9497

9598
```rust
9699
use sentry::integrations::tracing::EventFilter;
97100

98-
sentry::integrations::tracing::layer().event_filter(|md| match *md.level() {
99-
// Capture error level events as Sentry events
100-
tracing::Level::ERROR => EventFilter::Event,
101-
// Ignore trace level events, as they're too verbose
102-
tracing::Level::TRACE => EventFilter::Ignore,
103-
// Capture everything else as a structured log
104-
_ => EventFilter::Log,
105-
});
101+
let sentry_layer =
102+
sentry::integrations::tracing::layer().event_filter(|md| match *md.level() {
103+
// Capture error and warn level events as both logs and events in Sentry
104+
tracing::Level::ERROR => EventFilter::Event | EventFilter::Log,
105+
// Ignore trace level events, as they're too verbose
106+
tracing::Level::TRACE => EventFilter::Ignore,
107+
// Capture everything else as both a breadcrumb and a log
108+
_ => EventFilter::Breadcrumb | EventFilter::Log,
109+
});
106110
```
107111

108112
## Verify

0 commit comments

Comments
 (0)