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 = { version = "1.0.197", features = ["derive"] }
|
||||||
serde_derive = { version = "1.0.197" }
|
serde_derive = { version = "1.0.197" }
|
||||||
num_cpus = "1.16.0"
|
num_cpus = "1.16.0"
|
||||||
tracing = { version = "0.1.40", features = ["log"] }
|
fern = {version = "0.6.2", features = ["colored"]}
|
||||||
tracing-appender = { version = "0.2.3" }
|
log = "0.4.21"
|
||||||
tracing-subscriber = { version = "0.3.18", features = ["fmt", "tracing-log"] }
|
humantime = "2.1.0"
|
||||||
|
|||||||
@ -20,7 +20,7 @@ pub struct Args {
|
|||||||
)]
|
)]
|
||||||
log_level: Option<String>,
|
log_level: Option<String>,
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
log_file: Option<PathBuf>,
|
log_file: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Args {
|
impl Default for Args {
|
||||||
@ -59,7 +59,7 @@ impl Args {
|
|||||||
self.log_level.clone()
|
self.log_level.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_log_file(&self) -> Option<PathBuf> {
|
pub fn get_log_file(&self) -> Option<String> {
|
||||||
self.log_file.clone()
|
self.log_file.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,7 +20,7 @@ pub struct Config {
|
|||||||
pub engine: Option<Engine>,
|
pub engine: Option<Engine>,
|
||||||
|
|
||||||
pub log_level: Option<String>,
|
pub log_level: Option<String>,
|
||||||
pub log_file: Option<PathBuf>
|
pub log_file: Option<String>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
@ -32,7 +32,7 @@ impl Default for Config {
|
|||||||
engine: Some(Engine::default()),
|
engine: Some(Engine::default()),
|
||||||
|
|
||||||
log_level: Some("info".to_string()),
|
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;
|
mod config;
|
||||||
|
use std::str::FromStr;
|
||||||
mod args;
|
mod args;
|
||||||
mod engine;
|
mod engine;
|
||||||
|
|
||||||
|
use crate::logging::logger_init;
|
||||||
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
|
|
||||||
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use crate::logger_update;
|
|
||||||
|
|
||||||
use self::{args::Args, config::Config};
|
use self::{args::Args, config::Config};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use tracing::{debug, error, info, Level};
|
|
||||||
|
|
||||||
|
|
||||||
pub fn get_config() -> Option<Config> {
|
pub fn get_config() -> Option<Config> {
|
||||||
@ -20,8 +19,6 @@ pub fn get_config() -> Option<Config> {
|
|||||||
println!("{}", err);
|
println!("{}", err);
|
||||||
return Err(())
|
return Err(())
|
||||||
}
|
}
|
||||||
error!("{}", err);
|
|
||||||
info!("continuing withouth commandline arguments");
|
|
||||||
Ok(Args::default())
|
Ok(Args::default())
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -30,7 +27,7 @@ pub fn get_config() -> Option<Config> {
|
|||||||
Ok(args) => {
|
Ok(args) => {
|
||||||
let config_path = args.get_config_path().unwrap_or_default();
|
let config_path = args.get_config_path().unwrap_or_default();
|
||||||
let config = Config::read(&config_path).merge_args(&args);
|
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)
|
Some(config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,27 +1,18 @@
|
|||||||
use std::path::Path;
|
use std::time::SystemTime;
|
||||||
|
|
||||||
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");
|
|
||||||
|
|
||||||
}
|
pub fn logger_init(file: String, level: log::Level) -> Result<(), fern::InitError> {
|
||||||
|
fern::Dispatch::new()
|
||||||
pub fn logger_update(file: &Path, level: Level) {
|
.format(|out, message, record| {
|
||||||
let file_appender = tracing_appender::rolling::never(file.parent().unwrap(), file.file_name().unwrap());
|
out.finish(format_args!(
|
||||||
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
|
"[{} {} {}] {}",
|
||||||
println!("dir: {:?} | file: {:?}", file.parent(), file.file_name());
|
humantime::format_rfc3339_seconds(SystemTime::now()),
|
||||||
let subscriber = fmt()
|
record.level(),
|
||||||
.compact()
|
record.target(),
|
||||||
.with_max_level(level)
|
message
|
||||||
.with_writer(non_blocking)
|
))
|
||||||
.finish();
|
})
|
||||||
tracing::subscriber::set_global_default(subscriber)
|
.level(level.to_level_filter())
|
||||||
.expect("Failed to set global default subscriber");
|
.chain(fern::log_file(file)?)
|
||||||
|
.apply()?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,15 +9,11 @@ use crate::config::get_config;
|
|||||||
mod chess;
|
mod chess;
|
||||||
mod config;
|
mod config;
|
||||||
mod errors;
|
mod errors;
|
||||||
|
|
||||||
|
|
||||||
mod logging;
|
mod logging;
|
||||||
use crate::logging::*;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// logger_init();
|
|
||||||
let config = get_config();
|
let config = get_config();
|
||||||
if config.is_none() {
|
if config.is_none() {
|
||||||
return
|
return
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user