From 9957dc531d0ffae55460214263d50bd959c3373c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Th=C3=A4ter?= Date: Sat, 7 Aug 2021 09:32:18 +0200 Subject: [PATCH] Use a 'stderr' feature to target log output to stderr --- Cargo.toml | 1 + src/lib.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 214f303..9a0c408 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ edition = "2018" default = ["colored", "chrono"] colors = ["colored"] timestamps = ["chrono"] +stderr = [] [dependencies] log = { version = "^0.4.5", features = ["std"] } diff --git a/src/lib.rs b/src/lib.rs index 954ad6a..9e4a9ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -305,6 +305,7 @@ impl Log for SimpleLogger { #[cfg(feature = "chrono")] if self.timestamps { + #[cfg(not(feature = "stderr"))] println!( "{} {:<5} [{}] {}", Local::now().format("%Y-%m-%d %H:%M:%S,%3f"), @@ -312,10 +313,21 @@ impl Log for SimpleLogger { target, record.args() ); + #[cfg(feature = "stderr")] + eprintln!( + "{} {:<5} [{}] {}", + Local::now().format("%Y-%m-%d %H:%M:%S,%3f"), + level_string, + target, + record.args() + ); return; } + #[cfg(not(feature = "stderr"))] println!("{:<5} [{}] {}", level_string, target, record.args()); + #[cfg(feature = "stderr")] + eprintln!("{:<5} [{}] {}", level_string, target, record.args()); } }