slog_example_lib/
lib.rs

1//! Example of a library utilizing `slog` logging under the-hood but providing it's user with
2//! possibility to ignore `slog` functionality.
3#![warn(missing_docs)]
4
5/// Re-export slog
6///
7/// Users of this library can, but don't have to use slog to build their own
8/// loggers
9#[macro_use]
10pub extern crate slog ;
11extern crate slog_stdlog;
12
13use slog::Drain;
14
15/// MyLib main struct
16pub struct MyLib {
17    logger : slog::Logger,
18}
19
20impl MyLib {
21
22    /// Initialize `MyLib`, possibly providing custom logger
23    ///
24    /// `logger = None`, will make `MyLib` log to the `slog-stdlog`
25    /// drain. This make the library effectively work the same
26    /// as it was just using `log` instead of `slog`.
27    ///
28    /// `Into` trick allows passing `Logger` directly, without the `Some` part.
29    /// See http://xion.io/post/code/rust-optional-args.html
30    pub fn init<L : Into<Option<slog::Logger>>>(logger : L) -> Self {
31        MyLib {
32            logger: logger.into().unwrap_or(slog::Logger::root(slog_stdlog::StdLog.fuse(), o!())),
33        }
34    }
35
36    /// Do something
37    pub fn do_the_thing(&self) {
38        debug!(self.logger, "starting"; "what" => "the_thing");
39    }
40}