[−]Trait astral::thirdparty::slog::Drain
Logging drain
Drain
s typically mean destination for logs, but slog
generalizes the
term.
Drain
s are responsible for handling logging statements (Record
s) from
Logger
s associated with them: filtering, modifying, formatting
and writing the log records into given destination(s).
It's a typical pattern to parametrize Drain
s over Drain
traits to allow
composing Drain
s.
Implementing this trait allows writing custom Drain
s. Slog users should
not be afraid of implementing their own Drain
s. Any custom log handling
logic should be implemented as a Drain
.
Associated Types
type Ok
Type returned by this drain
It can be useful in some circumstances, but rarely. It will probably
default to ()
once https://github.com/rust-lang/rust/issues/29661 is
stable.
type Err
Type of potential errors that can be returned by this Drain
Required methods
fn log(
&self,
record: &Record,
values: &OwnedKVList
) -> Result<Self::Ok, Self::Err>
&self,
record: &Record,
values: &OwnedKVList
) -> Result<Self::Ok, Self::Err>
Handle one logging statement (Record
)
Every logging Record
built from a logging statement (eg.
info!(...)
), and key-value lists of a Logger
it was executed on
will be passed to the root drain registered during Logger::root
.
Typically Drain
s:
- pass this information (or not) to the sub-logger(s) (filters)
- format and write the information the a destination (writers)
- deal with the errors returned from the sub-logger(s)
Provided methods
fn is_enabled(&self, level: Level) -> bool
Avoid: Check if messages at the specified log level are maybe enabled for this logger.
The purpose of it so to allow imprecise detection if a given logging level has any chance of actually being logged. This might be used to explicitly skip needless computation.
It is best effort, can return false positives, but not false negatives.
The logger is still free to ignore records even if the level is enabled, so an enabled level doesn't necessarily guarantee that the record will actually be logged.
This function is somewhat needless, and is better expressed by using
lazy values (see FnValue
). A FnValue
is more precise and does not
require additional (potentially recursive) calls to do something that
log
will already do anyways (making decision if something should be
logged or not).
let logger = Logger::root(Discard, o!()); if logger.is_enabled(Level::Debug) { let num = 5.0f64; let sqrt = num.sqrt(); debug!(logger, "Sqrt"; "num" => num, "sqrt" => sqrt); }
fn is_critical_enabled(&self) -> bool
Avoid: See is_enabled
fn is_error_enabled(&self) -> bool
Avoid: See is_enabled
fn is_warning_enabled(&self) -> bool
Avoid: See is_enabled
fn is_info_enabled(&self) -> bool
Avoid: See is_enabled
fn is_debug_enabled(&self) -> bool
Avoid: See is_enabled
fn is_trace_enabled(&self) -> bool
Avoid: See is_enabled
fn map<F, R>(self, f: F) -> R where
F: FnOnce(Self) -> R,
F: FnOnce(Self) -> R,
Pass Drain
through a closure, eg. to wrap
into another Drain
.
#[macro_use] extern crate slog; use slog::*; fn main() { let _drain = Discard.map(Fuse); }
fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FilterFn,
F: FilterFn,
Filter logging records passed to Drain
Wrap Self
in Filter
This will convert self
to a Drain that ignores
Records for which
f` returns false.
fn filter_level(self, level: Level) -> LevelFilter<Self>
Filter logging records passed to Drain
(by level)
Wrap Self
in LevelFilter
This will convert self
to a Drain that ignores
Records of logging lever smaller than
level`.
fn map_err<F, E>(self, f: F) -> MapError<Self, E> where
F: MapErrFn<Self::Err, E>,
F: MapErrFn<Self::Err, E>,
Map logging errors returned by this drain
f
is a closure that takes Drain::Err
returned by a given
drain, and returns new error of potentially different type
fn ignore_res(self) -> IgnoreResult<Self>
Ignore results returned by this drain
Wrap Self
in IgnoreResult
fn fuse(self) -> Fuse<Self> where
Self::Err: Debug,
Self::Err: Debug,
Make Self
panic when returning any errors
Wrap Self
in Map
Implementations on Foreign Types
impl<'a, D> Drain for &'a D where
D: 'a + Drain,
D: 'a + Drain,
type Ok = <D as Drain>::Ok
type Err = <D as Drain>::Err
fn log(
&self,
record: &Record,
values: &OwnedKVList
) -> Result<<&'a D as Drain>::Ok, <&'a D as Drain>::Err>
&self,
record: &Record,
values: &OwnedKVList
) -> Result<<&'a D as Drain>::Ok, <&'a D as Drain>::Err>
fn is_enabled(&self, level: Level) -> bool
fn is_critical_enabled(&self) -> bool
fn is_error_enabled(&self) -> bool
fn is_warning_enabled(&self) -> bool
fn is_info_enabled(&self) -> bool
fn is_debug_enabled(&self) -> bool
fn is_trace_enabled(&self) -> bool
fn map<F, R>(self, f: F) -> R where
F: FnOnce(Self) -> R,
F: FnOnce(Self) -> R,
fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FilterFn,
F: FilterFn,
fn filter_level(self, level: Level) -> LevelFilter<Self>
fn map_err<F, E>(self, f: F) -> MapError<Self, E> where
F: MapErrFn<Self::Err, E>,
F: MapErrFn<Self::Err, E>,
fn ignore_res(self) -> IgnoreResult<Self>
fn fuse(self) -> Fuse<Self> where
Self::Err: Debug,
Self::Err: Debug,
impl<'a, D> Drain for &'a mut D where
D: 'a + Drain,
D: 'a + Drain,
type Ok = <D as Drain>::Ok
type Err = <D as Drain>::Err
fn log(
&self,
record: &Record,
values: &OwnedKVList
) -> Result<<&'a mut D as Drain>::Ok, <&'a mut D as Drain>::Err>
&self,
record: &Record,
values: &OwnedKVList
) -> Result<<&'a mut D as Drain>::Ok, <&'a mut D as Drain>::Err>
fn is_enabled(&self, level: Level) -> bool
fn is_critical_enabled(&self) -> bool
fn is_error_enabled(&self) -> bool
fn is_warning_enabled(&self) -> bool
fn is_info_enabled(&self) -> bool
fn is_debug_enabled(&self) -> bool
fn is_trace_enabled(&self) -> bool
fn map<F, R>(self, f: F) -> R where
F: FnOnce(Self) -> R,
F: FnOnce(Self) -> R,
fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FilterFn,
F: FilterFn,
fn filter_level(self, level: Level) -> LevelFilter<Self>
fn map_err<F, E>(self, f: F) -> MapError<Self, E> where
F: MapErrFn<Self::Err, E>,
F: MapErrFn<Self::Err, E>,
fn ignore_res(self) -> IgnoreResult<Self>
fn fuse(self) -> Fuse<Self> where
Self::Err: Debug,
Self::Err: Debug,
impl<D> Drain for Arc<D> where
D: Drain + ?Sized,
D: Drain + ?Sized,
type Ok = <D as Drain>::Ok
type Err = <D as Drain>::Err
fn log(
&self,
record: &Record,
o: &OwnedKVList
) -> Result<<Arc<D> as Drain>::Ok, <D as Drain>::Err>
&self,
record: &Record,
o: &OwnedKVList
) -> Result<<Arc<D> as Drain>::Ok, <D as Drain>::Err>
fn is_enabled(&self, level: Level) -> bool
fn is_critical_enabled(&self) -> bool
fn is_error_enabled(&self) -> bool
fn is_warning_enabled(&self) -> bool
fn is_info_enabled(&self) -> bool
fn is_debug_enabled(&self) -> bool
fn is_trace_enabled(&self) -> bool
fn map<F, R>(self, f: F) -> R where
F: FnOnce(Self) -> R,
F: FnOnce(Self) -> R,
fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FilterFn,
F: FilterFn,
fn filter_level(self, level: Level) -> LevelFilter<Self>
fn map_err<F, E>(self, f: F) -> MapError<Self, E> where
F: MapErrFn<Self::Err, E>,
F: MapErrFn<Self::Err, E>,
fn ignore_res(self) -> IgnoreResult<Self>
fn fuse(self) -> Fuse<Self> where
Self::Err: Debug,
Self::Err: Debug,
impl<D> Drain for Mutex<D> where
D: Drain,
D: Drain,
type Ok = <D as Drain>::Ok
type Err = MutexDrainError<D>
fn log(
&self,
record: &Record,
logger_values: &OwnedKVList
) -> Result<<Mutex<D> as Drain>::Ok, <Mutex<D> as Drain>::Err>
&self,
record: &Record,
logger_values: &OwnedKVList
) -> Result<<Mutex<D> as Drain>::Ok, <Mutex<D> as Drain>::Err>
fn is_enabled(&self, level: Level) -> bool
fn is_critical_enabled(&self) -> bool
fn is_error_enabled(&self) -> bool
fn is_warning_enabled(&self) -> bool
fn is_info_enabled(&self) -> bool
fn is_debug_enabled(&self) -> bool
fn is_trace_enabled(&self) -> bool
fn map<F, R>(self, f: F) -> R where
F: FnOnce(Self) -> R,
F: FnOnce(Self) -> R,
fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FilterFn,
F: FilterFn,
fn filter_level(self, level: Level) -> LevelFilter<Self>
fn map_err<F, E>(self, f: F) -> MapError<Self, E> where
F: MapErrFn<Self::Err, E>,
F: MapErrFn<Self::Err, E>,
fn ignore_res(self) -> IgnoreResult<Self>
fn fuse(self) -> Fuse<Self> where
Self::Err: Debug,
Self::Err: Debug,
impl<D> Drain for Box<D> where
D: Drain + ?Sized,
D: Drain + ?Sized,
type Ok = <D as Drain>::Ok
type Err = <D as Drain>::Err
fn log(
&self,
record: &Record,
o: &OwnedKVList
) -> Result<<Box<D> as Drain>::Ok, <D as Drain>::Err>
&self,
record: &Record,
o: &OwnedKVList
) -> Result<<Box<D> as Drain>::Ok, <D as Drain>::Err>
fn is_enabled(&self, level: Level) -> bool
fn is_critical_enabled(&self) -> bool
fn is_error_enabled(&self) -> bool
fn is_warning_enabled(&self) -> bool
fn is_info_enabled(&self) -> bool
fn is_debug_enabled(&self) -> bool
fn is_trace_enabled(&self) -> bool
fn map<F, R>(self, f: F) -> R where
F: FnOnce(Self) -> R,
F: FnOnce(Self) -> R,
fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FilterFn,
F: FilterFn,
fn filter_level(self, level: Level) -> LevelFilter<Self>
fn map_err<F, E>(self, f: F) -> MapError<Self, E> where
F: MapErrFn<Self::Err, E>,
F: MapErrFn<Self::Err, E>,
fn ignore_res(self) -> IgnoreResult<Self>
fn fuse(self) -> Fuse<Self> where
Self::Err: Debug,
Self::Err: Debug,
Implementors
impl Drain for Discard
type Ok = ()
type Err = NeverStruct
fn log(&self, &Record, &OwnedKVList) -> Result<(), NeverStruct>
fn is_enabled(&self, _1: Level) -> bool
fn is_critical_enabled(&self) -> bool
fn is_error_enabled(&self) -> bool
fn is_warning_enabled(&self) -> bool
fn is_info_enabled(&self) -> bool
fn is_debug_enabled(&self) -> bool
fn is_trace_enabled(&self) -> bool
fn map<F, R>(self, f: F) -> R where
F: FnOnce(Self) -> R,
F: FnOnce(Self) -> R,
fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FilterFn,
F: FilterFn,
fn filter_level(self, level: Level) -> LevelFilter<Self>
fn map_err<F, E>(self, f: F) -> MapError<Self, E> where
F: MapErrFn<Self::Err, E>,
F: MapErrFn<Self::Err, E>,
fn ignore_res(self) -> IgnoreResult<Self>
fn fuse(self) -> Fuse<Self> where
Self::Err: Debug,
Self::Err: Debug,
impl<D> Drain for Fuse<D> where
D: Drain,
<D as Drain>::Err: Debug,
D: Drain,
<D as Drain>::Err: Debug,
type Ok = ()
type Err = NeverStruct
fn log(
&self,
record: &Record,
logger_values: &OwnedKVList
) -> Result<<Fuse<D> as Drain>::Ok, NeverStruct>
&self,
record: &Record,
logger_values: &OwnedKVList
) -> Result<<Fuse<D> as Drain>::Ok, NeverStruct>
fn is_enabled(&self, level: Level) -> bool
fn is_critical_enabled(&self) -> bool
fn is_error_enabled(&self) -> bool
fn is_warning_enabled(&self) -> bool
fn is_info_enabled(&self) -> bool
fn is_debug_enabled(&self) -> bool
fn is_trace_enabled(&self) -> bool
fn map<F, R>(self, f: F) -> R where
F: FnOnce(Self) -> R,
F: FnOnce(Self) -> R,
fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FilterFn,
F: FilterFn,
fn filter_level(self, level: Level) -> LevelFilter<Self>
fn map_err<F, E>(self, f: F) -> MapError<Self, E> where
F: MapErrFn<Self::Err, E>,
F: MapErrFn<Self::Err, E>,
fn ignore_res(self) -> IgnoreResult<Self>
fn fuse(self) -> Fuse<Self> where
Self::Err: Debug,
Self::Err: Debug,
impl<D> Drain for IgnoreResult<D> where
D: Drain,
D: Drain,
type Ok = ()
type Err = NeverStruct
fn log(
&self,
record: &Record,
logger_values: &OwnedKVList
) -> Result<(), NeverStruct>
&self,
record: &Record,
logger_values: &OwnedKVList
) -> Result<(), NeverStruct>
fn is_enabled(&self, level: Level) -> bool
fn is_critical_enabled(&self) -> bool
fn is_error_enabled(&self) -> bool
fn is_warning_enabled(&self) -> bool
fn is_info_enabled(&self) -> bool
fn is_debug_enabled(&self) -> bool
fn is_trace_enabled(&self) -> bool
fn map<F, R>(self, f: F) -> R where
F: FnOnce(Self) -> R,
F: FnOnce(Self) -> R,
fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FilterFn,
F: FilterFn,
fn filter_level(self, level: Level) -> LevelFilter<Self>
fn map_err<F, E>(self, f: F) -> MapError<Self, E> where
F: MapErrFn<Self::Err, E>,
F: MapErrFn<Self::Err, E>,
fn ignore_res(self) -> IgnoreResult<Self>
fn fuse(self) -> Fuse<Self> where
Self::Err: Debug,
Self::Err: Debug,
impl<D> Drain for LevelFilter<D> where
D: Drain,
D: Drain,
type Ok = Option<<D as Drain>::Ok>
type Err = <D as Drain>::Err
fn log(
&self,
record: &Record,
logger_values: &OwnedKVList
) -> Result<<LevelFilter<D> as Drain>::Ok, <LevelFilter<D> as Drain>::Err>
&self,
record: &Record,
logger_values: &OwnedKVList
) -> Result<<LevelFilter<D> as Drain>::Ok, <LevelFilter<D> as Drain>::Err>
fn is_enabled(&self, level: Level) -> bool
fn is_critical_enabled(&self) -> bool
fn is_error_enabled(&self) -> bool
fn is_warning_enabled(&self) -> bool
fn is_info_enabled(&self) -> bool
fn is_debug_enabled(&self) -> bool
fn is_trace_enabled(&self) -> bool
fn map<F, R>(self, f: F) -> R where
F: FnOnce(Self) -> R,
F: FnOnce(Self) -> R,
fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FilterFn,
F: FilterFn,
fn filter_level(self, level: Level) -> LevelFilter<Self>
fn map_err<F, E>(self, f: F) -> MapError<Self, E> where
F: MapErrFn<Self::Err, E>,
F: MapErrFn<Self::Err, E>,
fn ignore_res(self) -> IgnoreResult<Self>
fn fuse(self) -> Fuse<Self> where
Self::Err: Debug,
Self::Err: Debug,
impl<D> Drain for Logger<D> where
D: SendSyncUnwindSafeDrain<Ok = (), Err = NeverStruct>,
D: SendSyncUnwindSafeDrain<Ok = (), Err = NeverStruct>,
type Ok = ()
type Err = NeverStruct
fn log(
&self,
record: &Record,
values: &OwnedKVList
) -> Result<<Logger<D> as Drain>::Ok, <Logger<D> as Drain>::Err>
&self,
record: &Record,
values: &OwnedKVList
) -> Result<<Logger<D> as Drain>::Ok, <Logger<D> as Drain>::Err>
fn is_enabled(&self, level: Level) -> bool
fn is_critical_enabled(&self) -> bool
fn is_error_enabled(&self) -> bool
fn is_warning_enabled(&self) -> bool
fn is_info_enabled(&self) -> bool
fn is_debug_enabled(&self) -> bool
fn is_trace_enabled(&self) -> bool
fn map<F, R>(self, f: F) -> R where
F: FnOnce(Self) -> R,
F: FnOnce(Self) -> R,
fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FilterFn,
F: FilterFn,
fn filter_level(self, level: Level) -> LevelFilter<Self>
fn map_err<F, E>(self, f: F) -> MapError<Self, E> where
F: MapErrFn<Self::Err, E>,
F: MapErrFn<Self::Err, E>,
fn ignore_res(self) -> IgnoreResult<Self>
fn fuse(self) -> Fuse<Self> where
Self::Err: Debug,
Self::Err: Debug,
impl<D, E> Drain for MapError<D, E> where
D: Drain,
D: Drain,
type Ok = <D as Drain>::Ok
type Err = E
fn log(
&self,
record: &Record,
logger_values: &OwnedKVList
) -> Result<<MapError<D, E> as Drain>::Ok, <MapError<D, E> as Drain>::Err>
&self,
record: &Record,
logger_values: &OwnedKVList
) -> Result<<MapError<D, E> as Drain>::Ok, <MapError<D, E> as Drain>::Err>
fn is_enabled(&self, level: Level) -> bool
fn is_critical_enabled(&self) -> bool
fn is_error_enabled(&self) -> bool
fn is_warning_enabled(&self) -> bool
fn is_info_enabled(&self) -> bool
fn is_debug_enabled(&self) -> bool
fn is_trace_enabled(&self) -> bool
fn map<F, R>(self, f: F) -> R where
F: FnOnce(Self) -> R,
F: FnOnce(Self) -> R,
fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FilterFn,
F: FilterFn,
fn filter_level(self, level: Level) -> LevelFilter<Self>
fn map_err<F, E>(self, f: F) -> MapError<Self, E> where
F: MapErrFn<Self::Err, E>,
F: MapErrFn<Self::Err, E>,
fn ignore_res(self) -> IgnoreResult<Self>
fn fuse(self) -> Fuse<Self> where
Self::Err: Debug,
Self::Err: Debug,
impl<D, F> Drain for Filter<D, F> where
D: Drain,
F: FilterFn,
D: Drain,
F: FilterFn,
type Ok = Option<<D as Drain>::Ok>
type Err = <D as Drain>::Err
fn log(
&self,
record: &Record,
logger_values: &OwnedKVList
) -> Result<<Filter<D, F> as Drain>::Ok, <Filter<D, F> as Drain>::Err>
&self,
record: &Record,
logger_values: &OwnedKVList
) -> Result<<Filter<D, F> as Drain>::Ok, <Filter<D, F> as Drain>::Err>
fn is_enabled(&self, level: Level) -> bool
fn is_critical_enabled(&self) -> bool
fn is_error_enabled(&self) -> bool
fn is_warning_enabled(&self) -> bool
fn is_info_enabled(&self) -> bool
fn is_debug_enabled(&self) -> bool
fn is_trace_enabled(&self) -> bool
fn map<F, R>(self, f: F) -> R where
F: FnOnce(Self) -> R,
F: FnOnce(Self) -> R,
fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FilterFn,
F: FilterFn,
fn filter_level(self, level: Level) -> LevelFilter<Self>
fn map_err<F, E>(self, f: F) -> MapError<Self, E> where
F: MapErrFn<Self::Err, E>,
F: MapErrFn<Self::Err, E>,
fn ignore_res(self) -> IgnoreResult<Self>
fn fuse(self) -> Fuse<Self> where
Self::Err: Debug,
Self::Err: Debug,
impl<D1, D2> Drain for Duplicate<D1, D2> where
D1: Drain,
D2: Drain,
D1: Drain,
D2: Drain,
type Ok = (<D1 as Drain>::Ok, <D2 as Drain>::Ok)
type Err = (Result<<D1 as Drain>::Ok, <D1 as Drain>::Err>, Result<<D2 as Drain>::Ok, <D2 as Drain>::Err>)
fn log(
&self,
record: &Record,
logger_values: &OwnedKVList
) -> Result<<Duplicate<D1, D2> as Drain>::Ok, <Duplicate<D1, D2> as Drain>::Err>
&self,
record: &Record,
logger_values: &OwnedKVList
) -> Result<<Duplicate<D1, D2> as Drain>::Ok, <Duplicate<D1, D2> as Drain>::Err>
fn is_enabled(&self, level: Level) -> bool
fn is_critical_enabled(&self) -> bool
fn is_error_enabled(&self) -> bool
fn is_warning_enabled(&self) -> bool
fn is_info_enabled(&self) -> bool
fn is_debug_enabled(&self) -> bool
fn is_trace_enabled(&self) -> bool
fn map<F, R>(self, f: F) -> R where
F: FnOnce(Self) -> R,
F: FnOnce(Self) -> R,
fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FilterFn,
F: FilterFn,
fn filter_level(self, level: Level) -> LevelFilter<Self>
fn map_err<F, E>(self, f: F) -> MapError<Self, E> where
F: MapErrFn<Self::Err, E>,
F: MapErrFn<Self::Err, E>,
fn ignore_res(self) -> IgnoreResult<Self>
fn fuse(self) -> Fuse<Self> where
Self::Err: Debug,
Self::Err: Debug,