Skip to content

Commit 53c2165

Browse files
authored
Merge branch 'master' into lcian/ref/log-default-logs
2 parents 71749b9 + 3b78cf8 commit 53c2165

File tree

6 files changed

+56
-35
lines changed

6 files changed

+56
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- feat(log): support combined LogFilters and RecordMappings ([#914](https://github.com/getsentry/sentry-rust/pull/914)) by @lcian
88
- `sentry::integrations::log::LogFilter` has been changed to a `bitflags` struct.
99
- It's now possible to map a `log` record to multiple items in Sentry by combining multiple log filters in the filter, e.g. `log::Level::ERROR => LogFilter::Event | LogFilter::Log`.
10-
- It's also possible to use `sentry::integrations::log::RecordMapping::Combined` to map a `log` record to multiple items in Sentry.
10+
- If using a custom `mapper` instead, it's possible to return a `Vec<sentry::integrations::log::RecordMapping>` to map a `log` record to multiple items in Sentry.
1111

1212
### Behavioral changes
1313

Cargo.lock

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sentry-log/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ logs = ["sentry-core/logs"]
1919
[dependencies]
2020
sentry-core = { version = "0.43.0", path = "../sentry-core" }
2121
log = { version = "0.4.8", features = ["std", "kv"] }
22-
bitflags = "2.0.0"
22+
bitflags = "2.9.4"
2323

2424
[dev-dependencies]
2525
sentry = { path = "../sentry", default-features = false, features = ["test"] }

sentry-log/src/lib.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,32 @@
5353
//! use sentry_log::LogFilter;
5454
//!
5555
//! let logger = sentry_log::SentryLogger::new().filter(|md| match md.level() {
56-
//! log::Level::Error => LogFilter::Event | LogFilter::Log,
56+
//! log::Level::Error => LogFilter::Event,
5757
//! log::Level::Warn => LogFilter::Breadcrumb | LogFilter::Log,
5858
//! _ => LogFilter::Ignore,
5959
//! });
6060
//! ```
6161
//!
62-
//! If you're using a custom record mapper instead of a filter, use `RecordMapping::Combined`.
62+
//! If you're using a custom record mapper instead of a filter, you can return a `Vec<RecordMapping>`
63+
//! from your mapper function to send multiple items to Sentry from a single log record:
64+
//!
65+
//! ```
66+
//! use sentry_log::{RecordMapping, SentryLogger, event_from_record, breadcrumb_from_record};
67+
//!
68+
//! let logger = SentryLogger::new().mapper(|record| {
69+
//! match record.level() {
70+
//! log::Level::Error => {
71+
//! // Send both an event and a breadcrumb for errors
72+
//! vec![
73+
//! RecordMapping::Event(event_from_record(record)),
74+
//! RecordMapping::Breadcrumb(breadcrumb_from_record(record)),
75+
//! ]
76+
//! }
77+
//! log::Level::Warn => RecordMapping::Breadcrumb(breadcrumb_from_record(record)).into(),
78+
//! _ => RecordMapping::Ignore.into(),
79+
//! }
80+
//! });
81+
//! ```
6382
6483
#![doc(html_favicon_url = "https://sentry-brand.storage.googleapis.com/favicon.ico")]
6584
#![doc(html_logo_url = "https://sentry-brand.storage.googleapis.com/sentry-glyph-black.png")]

sentry-log/src/logger.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ impl From<Vec<RecordMapping>> for CombinedRecordMapping {
6363
}
6464
}
6565

66+
impl From<RecordMapping> for Vec<RecordMapping> {
67+
fn from(mapping: RecordMapping) -> Self {
68+
vec![mapping]
69+
}
70+
}
71+
6672
/// The default log filter.
6773
///
6874
/// By default, an exception event is captured for `error`, a breadcrumb for
@@ -106,7 +112,7 @@ pub struct SentryLogger<L: log::Log> {
106112
dest: L,
107113
filter: Box<dyn Fn(&log::Metadata<'_>) -> LogFilter + Send + Sync>,
108114
#[allow(clippy::type_complexity)]
109-
mapper: Option<Box<dyn Fn(&Record<'_>) -> RecordMapping + Send + Sync>>,
115+
mapper: Option<Box<dyn Fn(&Record<'_>) -> Vec<RecordMapping> + Send + Sync>>,
110116
}
111117

112118
impl Default for SentryLogger<NoopLogger> {
@@ -152,13 +158,15 @@ impl<L: log::Log> SentryLogger<L> {
152158
/// Sets a custom mapper function.
153159
///
154160
/// The mapper is responsible for creating either breadcrumbs or events
155-
/// from [`Record`]s.
161+
/// from [`Record`]s. It can return either a single [`RecordMapping`] or
162+
/// a `Vec<RecordMapping>` to send multiple items to Sentry from one log record.
156163
#[must_use]
157-
pub fn mapper<M>(mut self, mapper: M) -> Self
164+
pub fn mapper<M, T>(mut self, mapper: M) -> Self
158165
where
159-
M: Fn(&Record<'_>) -> RecordMapping + Send + Sync + 'static,
166+
M: Fn(&Record<'_>) -> T + Send + Sync + 'static,
167+
T: Into<Vec<RecordMapping>>,
160168
{
161-
self.mapper = Some(Box::new(mapper));
169+
self.mapper = Some(Box::new(move |record| mapper(record).into()));
162170
self
163171
}
164172
}
@@ -169,7 +177,7 @@ impl<L: log::Log> log::Log for SentryLogger<L> {
169177
}
170178

171179
fn log(&self, record: &log::Record<'_>) {
172-
let items: RecordMapping = match &self.mapper {
180+
let items = match &self.mapper {
173181
Some(mapper) => mapper(record),
174182
None => {
175183
let filter = (self.filter)(record.metadata());
@@ -187,13 +195,12 @@ impl<L: log::Log> log::Log for SentryLogger<L> {
187195
if filter.contains(LogFilter::Log) {
188196
items.push(RecordMapping::Log(log_from_record(record)));
189197
}
190-
RecordMapping::Combined(CombinedRecordMapping(items))
198+
items
191199
}
192200
};
193-
let items = CombinedRecordMapping::from(items);
194201

195-
for item in items.0 {
196-
match item {
202+
for mapping in items {
203+
match mapping {
197204
RecordMapping::Ignore => {}
198205
RecordMapping::Breadcrumb(breadcrumb) => sentry_core::add_breadcrumb(breadcrumb),
199206
RecordMapping::Event(event) => {
@@ -203,11 +210,6 @@ impl<L: log::Log> log::Log for SentryLogger<L> {
203210
RecordMapping::Log(log) => {
204211
sentry_core::Hub::with_active(|hub| hub.capture_log(log))
205212
}
206-
RecordMapping::Combined(_) => {
207-
sentry_core::sentry_debug!(
208-
"[SentryLogger] found nested CombinedEventMapping, ignoring"
209-
)
210-
}
211213
}
212214
}
213215

sentry-tracing/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ tracing-subscriber = { version = "0.3.20", default-features = false, features =
2929
"std",
3030
] }
3131
sentry-backtrace = { version = "0.43.0", path = "../sentry-backtrace", optional = true }
32-
bitflags = "2.0.0"
32+
bitflags = "2.9.4"
3333

3434
[dev-dependencies]
3535
log = "0.4"

0 commit comments

Comments
 (0)