use std::sync::{Mutex, Once};
use lazy_static::lazy_static;
use log::Level;
use slog::{o, Drain};
lazy_static! {
static ref LOGGER: slog::Logger = slog::Logger::root(
Mutex::new(slog_json::Json::default(std::io::stderr())).map(slog::Fuse),
o!()
);
static ref INIT_GLOBAL_LOGGER: Once = Once::new();
static ref GLOBAL_LOGGER_SCOPE_GUARD: Mutex<Option<slog_scope::GlobalLoggerGuard>> = Mutex::new(None);
}
pub fn get_logger(module: &'static str) -> slog::Logger {
LOGGER.new(o!("module" => module))
}
pub fn init_logger(level: Level) {
INIT_GLOBAL_LOGGER.call_once(|| {
let global_logger = LOGGER.new(o!("module" => "global"));
GLOBAL_LOGGER_SCOPE_GUARD
.lock()
.unwrap()
.get_or_insert(slog_scope::set_global_logger(global_logger));
slog_stdlog::init_with_level(level).unwrap();
});
}