Skip to content

Commit

Permalink
Use a 'stderr' feature to target log output to stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
cehteh committed Aug 7, 2021
1 parent 3dd34b0 commit 9957dc5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ edition = "2018"
default = ["colored", "chrono"]
colors = ["colored"]
timestamps = ["chrono"]
stderr = []

[dependencies]
log = { version = "^0.4.5", features = ["std"] }
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,29 @@ 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"),
level_string,
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());
}
}

Expand Down

0 comments on commit 9957dc5

Please sign in to comment.