Skip to content

Commit

Permalink
fix(logging-toolkit): Use synchronous logger
Browse files Browse the repository at this point in the history
The asynchronous logger was logging panics but didn't fail on them.

Fixes #551.
  • Loading branch information
laser authored and vmx committed Apr 26, 2019
1 parent 51d90ea commit b8d2ce5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
1 change: 0 additions & 1 deletion logging-toolkit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ edition = "2018"
slog = "2.4.1"
slog-term = "2.4.0"
slog-json = "2.3.0"
slog-async = "2.3.0"
lazy_static = "1.2"
57 changes: 27 additions & 30 deletions logging-toolkit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#[macro_use]
extern crate slog;
extern crate slog_async;
extern crate slog_json;
extern crate slog_term;
#[macro_use]
Expand All @@ -19,23 +18,6 @@ lazy_static! {
}

pub fn make_root_logger(use_json_env_name: &str, min_log_level_env_name: &str) -> Logger {
let drain = match env::var(use_json_env_name).as_ref().map(String::as_str) {
Ok("true") => {
let json_drain = slog_json::Json::new(std::io::stdout())
.add_default_keys()
.build()
.fuse();

slog_async::Async::new(json_drain).build().fuse()
}
_ => {
let term_decorator = slog_term::TermDecorator::new().build();
let term_drain = slog_term::FullFormat::new(term_decorator).build().fuse();

slog_async::Async::new(term_drain).build().fuse()
}
};

let min_log_level = match env::var(min_log_level_env_name) {
Ok(val) => match val.parse::<u64>() {
Ok(parsed) => match Level::from_usize(parsed as usize) {
Expand All @@ -47,18 +29,33 @@ pub fn make_root_logger(use_json_env_name: &str, min_log_level_env_name: &str) -
_ => Level::Info,
};

let with_filter = LevelFilter::new(drain, min_log_level).map(slog::Fuse);

Logger::root(
with_filter,
o!("place" => FnValue(move |info| {
format!("{}:{} {}",
info.file(),
info.line(),
info.module(),
)
})),
)
let logging_config = o!("place" => FnValue(move |info| {
format!("{}:{} {}",
info.file(),
info.line(),
info.module(),
)
}));

let use_json_logger = env::var(use_json_env_name)
.map(|x| x == "true")
.unwrap_or(false);

if use_json_logger {
let formatted = slog_json::Json::new(std::io::stdout())
.add_default_keys()
.build()
.fuse();
let mutexed = std::sync::Mutex::new(formatted).fuse();
let filtered = LevelFilter::new(mutexed, min_log_level).map(slog::Fuse);
Logger::root(filtered, logging_config)
} else {
let term_decorator = slog_term::TermDecorator::new().build();
let formatted = slog_term::FullFormat::new(term_decorator).build().fuse();
let mutexed = std::sync::Mutex::new(formatted).fuse();
let filtered = LevelFilter::new(mutexed, min_log_level).map(slog::Fuse);
Logger::root(filtered, logging_config)
}
}

pub fn make_logger(root_name: &'static str) -> Logger {
Expand Down

0 comments on commit b8d2ce5

Please sign in to comment.