From ad1fab1aa2ccb7ee32bda1e9006f746c8038b800 Mon Sep 17 00:00:00 2001 From: fabolous005 Date: Fri, 12 Apr 2024 12:07:05 +0200 Subject: [PATCH] use fern instead of tracing --- Cargo.toml | 6 +++--- src/config/args.rs | 4 ++-- src/config/config.rs | 4 ++-- src/config/mod.rs | 11 ++++------- src/logging.rs | 41 ++++++++++++++++------------------------- src/main.rs | 4 ---- 6 files changed, 27 insertions(+), 43 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a76725d..b2c46f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,6 @@ clap = { version = "4.5.4", features = ["derive"] } serde = { version = "1.0.197", features = ["derive"] } serde_derive = { version = "1.0.197" } num_cpus = "1.16.0" -tracing = { version = "0.1.40", features = ["log"] } -tracing-appender = { version = "0.2.3" } -tracing-subscriber = { version = "0.3.18", features = ["fmt", "tracing-log"] } +fern = {version = "0.6.2", features = ["colored"]} +log = "0.4.21" +humantime = "2.1.0" diff --git a/src/config/args.rs b/src/config/args.rs index d3dba35..a79d49b 100644 --- a/src/config/args.rs +++ b/src/config/args.rs @@ -20,7 +20,7 @@ pub struct Args { )] log_level: Option, #[arg(long)] - log_file: Option, + log_file: Option, } impl Default for Args { @@ -59,7 +59,7 @@ impl Args { self.log_level.clone() } - pub fn get_log_file(&self) -> Option { + pub fn get_log_file(&self) -> Option { self.log_file.clone() } } diff --git a/src/config/config.rs b/src/config/config.rs index fa80380..8205437 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -20,7 +20,7 @@ pub struct Config { pub engine: Option, pub log_level: Option, - pub log_file: Option + pub log_file: Option } impl Default for Config { @@ -32,7 +32,7 @@ impl Default for Config { engine: Some(Engine::default()), log_level: Some("info".to_string()), - log_file: Some(PathBuf::from("saltfish.log")), + log_file: Some("saltfish.log".to_string()), } } } diff --git a/src/config/mod.rs b/src/config/mod.rs index c050e4f..cb5d353 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,17 +1,16 @@ mod config; +use std::str::FromStr; mod args; mod engine; +use crate::logging::logger_init; + pub mod error; -use std::str::FromStr; - -use crate::logger_update; use self::{args::Args, config::Config}; use clap::Parser; -use tracing::{debug, error, info, Level}; pub fn get_config() -> Option { @@ -20,8 +19,6 @@ pub fn get_config() -> Option { println!("{}", err); return Err(()) } - error!("{}", err); - info!("continuing withouth commandline arguments"); Ok(Args::default()) }); @@ -30,7 +27,7 @@ pub fn get_config() -> Option { Ok(args) => { let config_path = args.get_config_path().unwrap_or_default(); let config = Config::read(&config_path).merge_args(&args); - logger_update(&config.log_file.clone().unwrap(), Level::from_str(&config.log_level.clone().unwrap()).unwrap()); + logger_init(config.log_file.clone().unwrap(), log::Level::from_str(&config.log_level.clone().unwrap()).unwrap()).unwrap(); Some(config) } } diff --git a/src/logging.rs b/src/logging.rs index 0a97abd..769f02c 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -1,27 +1,18 @@ -use std::path::Path; - -use tracing::Level; -use tracing_subscriber::fmt; - -pub fn logger_init() { - let subscriber = fmt() - .compact() - .with_max_level(Level::DEBUG) - .finish(); - // tracing::subscriber::set_global_default(subscriber) - // .expect("Failed to set global default subscriber"); +use std::time::SystemTime; -} - -pub fn logger_update(file: &Path, level: Level) { - let file_appender = tracing_appender::rolling::never(file.parent().unwrap(), file.file_name().unwrap()); - let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender); - println!("dir: {:?} | file: {:?}", file.parent(), file.file_name()); - let subscriber = fmt() - .compact() - .with_max_level(level) - .with_writer(non_blocking) - .finish(); - tracing::subscriber::set_global_default(subscriber) - .expect("Failed to set global default subscriber"); +pub fn logger_init(file: String, level: log::Level) -> Result<(), fern::InitError> { + fern::Dispatch::new() + .format(|out, message, record| { + out.finish(format_args!( + "[{} {} {}] {}", + humantime::format_rfc3339_seconds(SystemTime::now()), + record.level(), + record.target(), + message + )) + }) + .level(level.to_level_filter()) + .chain(fern::log_file(file)?) + .apply()?; + Ok(()) } diff --git a/src/main.rs b/src/main.rs index 902b6e8..54126c2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,15 +9,11 @@ use crate::config::get_config; mod chess; mod config; mod errors; - - mod logging; -use crate::logging::*; fn main() { - // logger_init(); let config = get_config(); if config.is_none() { return