sentry_log/
lib.rs

1//! Adds support for automatic Breadcrumb, Event, and Log capturing from `log` records.
2//!
3//! The `log` crate is supported in three ways:
4//! - Records can be captured as Sentry events. These are grouped and show up in the Sentry
5//!   [issues](https://docs.sentry.io/product/issues/) page, representing high severity issues to be
6//!   acted upon.
7//! - Records can be captured as [breadcrumbs](https://docs.sentry.io/product/issues/issue-details/breadcrumbs/).
8//!   Breadcrumbs create a trail of what happened prior to an event, and are therefore sent only when
9//!   an event is captured, either manually through e.g. `sentry::capture_message` or through integrations
10//!   (e.g. the panic integration is enabled (default) and a panic happens).
11//! - Records can be captured as traditional [logs](https://docs.sentry.io/product/explore/logs/)
12//!   Logs can be viewed and queried in the Logs explorer.
13//!
14//! By default anything at or above `Info` is recorded as a breadcrumb and
15//! anything at or above `Error` is captured as error event.
16//! Additionally, if the `sentry` crate is used with the `logs` feature flag, anything at or above `Info`
17//! is captured as a [Structured Log](https://docs.sentry.io/product/explore/logs/).
18//!
19//! # Examples
20//!
21//! ```
22//! let mut log_builder = pretty_env_logger::formatted_builder();
23//! log_builder.parse_filters("info");
24//! let logger = sentry_log::SentryLogger::with_dest(log_builder.build());
25//!
26//! log::set_boxed_logger(Box::new(logger)).unwrap();
27//! log::set_max_level(log::LevelFilter::Info);
28//!
29//! let _sentry = sentry::init(());
30//!
31//! log::info!("Generates a breadcrumb");
32//! log::error!("Generates an event");
33//! ```
34//!
35//! Or one might also set an explicit filter, to customize how to treat log
36//! records:
37//!
38//! ```
39//! use sentry_log::LogFilter;
40//!
41//! let logger = sentry_log::SentryLogger::new().filter(|md| match md.level() {
42//!     log::Level::Error => LogFilter::Event,
43//!     _ => LogFilter::Ignore,
44//! });
45//! ```
46//!
47//! # Sending multiple items to Sentry
48//!
49//! To map a log record to multiple items in Sentry, you can combine multiple log filters
50//! using the bitwise or operator:
51//!
52//! ```
53//! use sentry_log::LogFilter;
54//!
55//! let logger = sentry_log::SentryLogger::new().filter(|md| match md.level() {
56//!     log::Level::Error => LogFilter::Event,
57//!     log::Level::Warn => LogFilter::Breadcrumb | LogFilter::Log,
58//!     _ => LogFilter::Ignore,
59//! });
60//! ```
61//!
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//! ```
82
83#![doc(html_favicon_url = "https://sentry-brand.storage.googleapis.com/favicon.ico")]
84#![doc(html_logo_url = "https://sentry-brand.storage.googleapis.com/sentry-glyph-black.png")]
85#![warn(missing_docs)]
86
87mod converters;
88mod logger;
89
90pub use converters::*;
91pub use logger::*;