oasis_core_runtime/common/
logger.rs

1//! Logging subsystem for runtimes.
2use std::sync::{Mutex, Once};
3
4use lazy_static::lazy_static;
5use log::Level;
6use slog::{o, Drain};
7
8lazy_static! {
9    static ref LOGGER: slog::Logger = slog::Logger::root(
10        Mutex::new(slog_json::Json::default(std::io::stderr())).map(slog::Fuse),
11        o!()
12    );
13
14    /// Initializes the global logger once.
15    static ref INIT_GLOBAL_LOGGER: Once = Once::new();
16
17    /// Prevents the global logger from being dropped.
18    static ref GLOBAL_LOGGER_SCOPE_GUARD: Mutex<Option<slog_scope::GlobalLoggerGuard>> = Mutex::new(None);
19}
20
21/// Get the logger.
22pub fn get_logger(module: &'static str) -> slog::Logger {
23    LOGGER.new(o!("module" => module))
24}
25
26/// Initialize the global slog_stdlog adapter to allow logging with the log crate (instead of slog).
27pub fn init_logger(level: Level) {
28    INIT_GLOBAL_LOGGER.call_once(|| {
29        let global_logger = LOGGER.new(o!("module" => "global"));
30        GLOBAL_LOGGER_SCOPE_GUARD
31            .lock()
32            .unwrap()
33            .get_or_insert(slog_scope::set_global_logger(global_logger));
34        slog_stdlog::init_with_level(level).unwrap();
35    });
36}