use fern instead of tracing
This commit is contained in:
parent
4cabecea95
commit
ad1fab1aa2
@ -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"
|
||||
|
||||
@ -20,7 +20,7 @@ pub struct Args {
|
||||
)]
|
||||
log_level: Option<String>,
|
||||
#[arg(long)]
|
||||
log_file: Option<PathBuf>,
|
||||
log_file: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for Args {
|
||||
@ -59,7 +59,7 @@ impl Args {
|
||||
self.log_level.clone()
|
||||
}
|
||||
|
||||
pub fn get_log_file(&self) -> Option<PathBuf> {
|
||||
pub fn get_log_file(&self) -> Option<String> {
|
||||
self.log_file.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ pub struct Config {
|
||||
pub engine: Option<Engine>,
|
||||
|
||||
pub log_level: Option<String>,
|
||||
pub log_file: Option<PathBuf>
|
||||
pub log_file: Option<String>
|
||||
}
|
||||
|
||||
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()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<Config> {
|
||||
@ -20,8 +19,6 @@ pub fn get_config() -> Option<Config> {
|
||||
println!("{}", err);
|
||||
return Err(())
|
||||
}
|
||||
error!("{}", err);
|
||||
info!("continuing withouth commandline arguments");
|
||||
Ok(Args::default())
|
||||
});
|
||||
|
||||
@ -30,7 +27,7 @@ pub fn get_config() -> Option<Config> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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(())
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user