Struct slog::Logger
[−]
[src]
pub struct Logger<D = Arc<SendSyncRefUnwindSafeDrain<Ok=(), Err=Never>>> where D: SendSyncUnwindSafeDrain<Ok=(), Err=Never> { /* fields omitted */ }
Logging handle used to execute logging statements
In an essence Logger instance holds two pieces of information:
Drain- destination where to forward loggingRecords for processing.- Context - list of key-value pairs associated with it.
Root Logger is created with a Drain that will be cloned to every
member of it's hierarchy.
Child Logger are built from existing ones, and inherit their key-value
pairs, which can be supplemented with additional ones.
Cloning existing loggers and creating new ones is cheap. Loggers can be freely passed around the code and between threads.
Loggers are Sync+Send - there's no need to synchronize accesses to them,
as they can accept logging records from multiple threads at once. They can
be sent to any thread. Because of that they require the Drain to be
Sync+Sync as well. Not all Drains are Sync or Send but they can
often be made so by wrapping in a Mutex and/or Arc.
Logger implements Drain trait. Any logging Record delivered to
a Logger functioning as a Drain, will be delivered to it's Drain
with existing key-value pairs appended to the Loggers key-value pairs.
By itself it's effectively very similar to Logger being an ancestor
of Logger that originated the logging Record. Combined with other
Drains, allows custom processing logic for a sub-tree of a whole logging
tree.
Logger is parametrized over type of a Drain associated with it (D). It
default to type-erased version so Logger without any type annotation
means Logger<Arc<SendSyncRefUnwindSafeDrain<Ok = (), Err = Never>>>. See
Logger::to_erased for more information.
Methods
impl<D> Logger<D> where D: SendSyncUnwindSafeDrain<Ok=(), Err=Never>[src]
fn root<T>(drain: D, values: OwnedKV<T>) -> Logger<D> where D: 'static + SendSyncUnwindSafeDrain<Err=Never, Ok=()> + Sized,
T: ThreadSafeKV + 'static
T: ThreadSafeKV + 'static
Build a root Logger
All children and their children and so on form one logging tree sharing a common drain.
Root logger starts a new tree associated with a given Drain. Root
logger drain must return no errors. See Drain::ignore_res() and
Drain::fuse().
Use o! macro to build OwnedKV object.
#[macro_use] extern crate slog; fn main() { let _root = slog::Logger::root( slog::Discard, o!("key1" => "value1", "key2" => "value2"), ); }
fn new<T>(&self, values: OwnedKV<T>) -> Logger<D> where T: ThreadSafeKV + 'static, D: Clone
Build a child logger
Child logger inherits all existing key-value pairs from its parent and supplements them with additional ones.
Use o! macro to build OwnedKV object.
Drain cloning (D : Clone requirement)
All children, their children and so on, form one tree sharing a
common drain. This drain, will be Cloned when this method is called.
That is why Clone must be implemented for D in Logger<D>::new.
For some Drain types Clone is cheap or even free (a no-op). For
others cloning might be expensive (as they contain a lot of data), or
even impossible. In situations like that wrapping Drain in a
std::sync::Arc makes it Cloneable. Another way is calling
Logger::to_erased.
The reason why wrapping in an Arc is not done internally, and exposed
to the user is performance. Calling Drain::log through an Arc is
tiny bit slower than doing it directly.
#[macro_use] extern crate slog; fn main() { let root = slog::Logger::root(slog::Discard, o!("key1" => "value1", "key2" => "value2")); let _log = root.new(o!("key" => "value")); }
fn log(&self, record: &Record)
Log one logging Record
Use specific logging functions instead. See log! macro
documentation.
fn list(&self) -> &OwnedKVList
Get list of key-value pairs assigned to this Logger
fn into_erased(self)
-> Logger<Arc<SendSyncRefUnwindSafeDrain<Ok=(), Err=Never>>> where D: SendRefUnwindSafeDrain + 'static
-> Logger<Arc<SendSyncRefUnwindSafeDrain<Ok=(), Err=Never>>> where D: SendRefUnwindSafeDrain + 'static
Convert to default, "erased" type: Logger<Arc<SendSyncUnwindSafeDrain>>
Useful to adapt Logger<D : Clone> to an interface expecting
Logger<Arc<...>>.
Note that calling on a Logger<Arc<...>> will convert it to
Logger<Arc<Arc<...>>> which is not optimal. This might be fixed when
Rust gains trait implementation specialization.
fn to_erased(self) -> Logger<Arc<SendSyncRefUnwindSafeDrain<Ok=(), Err=Never>>> where D: SendRefUnwindSafeDrain + 'static + Clone
Create a copy with "erased" type
See into_erased
Trait Implementations
impl<D: Clone> Clone for Logger<D> where D: SendSyncUnwindSafeDrain<Ok=(), Err=Never>[src]
fn clone(&self) -> Logger<D>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)1.0.0
Performs copy-assignment from source. Read more
impl<D> Debug for Logger<D> where D: SendSyncUnwindSafeDrain<Ok=(), Err=Never>[src]
impl<D> Drain for Logger<D> where D: SendSyncUnwindSafeDrain<Ok=(), Err=Never>[src]
type Ok = ()
Type returned by this drain Read more
type Err = Never
Type of potential errors that can be returned by this Drain
fn log(&self,
record: &Record,
values: &OwnedKVList)
-> Result<Self::Ok, Self::Err>
record: &Record,
values: &OwnedKVList)
-> Result<Self::Ok, Self::Err>
Handle one logging statement (Record) Read more
fn map<F, R>(self, f: F) -> R where Self: Sized, F: FnOnce(Self) -> R
Pass Drain through a closure, eg. to wrap into another Drain. Read more
fn filter<F>(self, f: F) -> Filter<Self, F> where Self: Sized, F: Fn(&Record) -> bool + 'static + Send + Sync
Filter logging records passed to Drain Read more
fn filter_level(self, level: Level) -> LevelFilter<Self> where Self: Sized
Filter logging records passed to Drain (by level) Read more
fn map_err<F, E>(self, f: F) -> MapError<Self, E> where Self: Sized, F: 'static + Sync + Send + Fn(Self::Err) -> E
Map logging errors returned by this drain Read more
fn ignore_res(self) -> IgnoreResult<Self> where Self: Sized
Ignore results returned by this drain Read more
fn fuse(self) -> Fuse<Self> where Self::Err: Debug, Self: Sized
Make Self panic when returning any errors Read more