spdlog/sink/mod.rs
1//! Provides sinks to flexibly output log messages to specified targets.
2//!
3//! # Sink
4//!
5//! Sinks are the objects that actually write logs to their targets. Each sink
6//! should be responsible for only single target (e.g file, console, database),
7//! and each sink has its own private instance of [`Formatter`] object.
8//!
9//! A sink has its own level filter that is not shared with the logger, and a
10//! [`Logger`] can combine multiple [`Sink`]s.
11//!
12//! # Combined sink
13//!
14//! A combined sink is also a sink, but instead of having its own target and
15//! formatter, it combines other sinks (as sub-sinks).
16//!
17//! Operations on a combined sink will be forwarded to its sub-sinks according
18//! to the implementation.
19//!
20//! [`Logger`]: crate::logger::Logger
21
22#[cfg(feature = "multi-thread")]
23pub(crate) mod async_sink;
24mod dedup_sink;
25mod file_sink;
26mod helper;
27#[cfg(any(
28 all(target_os = "linux", feature = "native", feature = "libsystemd"),
29 all(doc, not(doctest))
30))]
31mod journald_sink;
32mod rotating_file_sink;
33mod std_stream_sink;
34#[cfg(any(all(windows, feature = "native"), all(doc, not(doctest))))]
35mod win_debug_sink;
36mod write_sink;
37
38#[cfg(feature = "multi-thread")]
39pub use async_sink::*;
40pub use dedup_sink::*;
41pub use file_sink::*;
42#[cfg(any(
43 all(target_os = "linux", feature = "native", feature = "libsystemd"),
44 all(doc, not(doctest))
45))]
46pub use journald_sink::*;
47pub use rotating_file_sink::*;
48pub use std_stream_sink::*;
49#[cfg(any(all(windows, feature = "native"), all(doc, not(doctest))))]
50pub use win_debug_sink::*;
51pub use write_sink::*;
52
53use crate::{formatter::Formatter, sync::*, ErrorHandler, Level, LevelFilter, Record, Result};
54
55/// Represents a sink
56pub trait Sink: Sync + Send {
57 /// Determines if a log message with the specified level would be logged.
58 #[must_use]
59 fn should_log(&self, level: Level) -> bool {
60 self.level_filter().test(level)
61 }
62
63 /// Logs a record.
64 fn log(&self, record: &Record) -> Result<()>;
65
66 /// Flushes any buffered records.
67 fn flush(&self) -> Result<()>;
68
69 /// Gets the log level filter.
70 #[must_use]
71 fn level_filter(&self) -> LevelFilter;
72
73 /// Sets the log level filter.
74 fn set_level_filter(&self, level_filter: LevelFilter);
75
76 /// Sets the formatter.
77 fn set_formatter(&self, formatter: Box<dyn Formatter>);
78
79 /// Sets a error handler.
80 ///
81 /// Most errors that occur in `Sink` will be returned as directly as
82 /// possible (e.g. returned to [`Logger`]), but some errors that cannot be
83 /// returned immediately, this function will be called. For example,
84 /// asynchronous errors.
85 ///
86 /// If no handler is set, [default error handler] will be used.
87 ///
88 /// [`Logger`]: crate::logger::Logger
89 /// [default error handler]: ../error/index.html#default-error-handler
90 fn set_error_handler(&self, handler: Option<ErrorHandler>);
91}
92
93/// Container type for [`Sink`]s.
94pub type Sinks = Vec<Arc<dyn Sink>>;