better logging handling

This commit is contained in:
Robin Müller 2024-04-16 15:03:10 +02:00
parent 2480ee6e06
commit e0c583cca8
3 changed files with 16 additions and 5 deletions

7
Cargo.lock generated
View File

@ -239,6 +239,12 @@ version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "iana-time-zone"
version = "0.1.60"
@ -376,6 +382,7 @@ dependencies = [
"chrono",
"derive-new",
"fern",
"humantime",
"lazy_static",
"log",
"num_enum",

View File

@ -10,6 +10,7 @@ fern = "0.6"
chrono = "0.4"
log = "0.4"
lazy_static = "1"
humantime = "2"
strum = { version = "0.26", features = ["derive"] }
thiserror = "1"
derive-new = "0.6"

View File

@ -1,17 +1,20 @@
pub fn setup_logger() -> Result<(), fern::InitError> {
fern::Dispatch::new()
.format(|out, message, record| {
.format(move |out, message, record| {
out.finish(format_args!(
"{}[{}][{}] {}",
chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"),
std::thread::current().name().expect("unnamed_thread"),
"[{}][{}][{}] {}",
humantime::format_rfc3339_millis(std::time::SystemTime::now()),
std::thread::current().name().unwrap_or("unnamed_thread"),
record.level(),
message
))
})
.level(log::LevelFilter::Debug)
.chain(std::io::stdout())
.chain(fern::log_file("output.log")?)
.chain(fern::log_file(format!(
"output_{}.log",
humantime::format_rfc3339_seconds(std::time::SystemTime::now())
))?)
.apply()?;
Ok(())
}