From 8160e72d82e1edc5b5b7fd41190c2823647fc561 Mon Sep 17 00:00:00 2001 From: zxj Date: Wed, 13 Nov 2024 10:43:07 +0800 Subject: [PATCH] bump v1.7.5 --- Cargo.toml | 4 +- src/config.rs | 23 +++++++--- src/fast_log.rs | 78 ++++++++++++++++++-------------- src/plugin/file_split.rs | 96 ++++++++++++++++++++++------------------ src/plugin/packer.rs | 33 +++----------- 5 files changed, 124 insertions(+), 110 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 827d611..47f5f0b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,9 +6,9 @@ members = [ [package] name = "fast_log" -version = "1.7.4" +version = "1.7.5" description = "Rust async log High-performance asynchronous logging" -readme = "Readme.md" +readme = "README.md" authors = ["ce "] repository = "https://github.com/rbatis/fast_log" edition = "2021" diff --git a/src/config.rs b/src/config.rs index c65a523..60bd8f4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,7 +4,9 @@ use crate::filter::Filter; use crate::plugin::console::ConsoleAppender; use crate::plugin::file::FileAppender; use crate::plugin::file_loop::FileLoopAppender; -use crate::plugin::file_split::{FileSplitAppender, CanRollingPack, Keep, Packer, RawFile, SplitFile}; +use crate::plugin::file_split::{ + CanRollingPack, FileSplitAppender, Keep, Packer, RawFile, SplitFile, +}; use crate::FastLogFormat; use dark_std::sync::SyncVec; use log::LevelFilter; @@ -106,7 +108,7 @@ impl Config { R: CanRollingPack + 'static, K: Keep + 'static, P: Packer + Sync + 'static, - >( + >( self, file_path: &str, rolling: R, @@ -120,7 +122,7 @@ impl Config { Box::new(keeper), Box::new(packer), ) - .unwrap(), + .expect("new split file fail"), ))); self } @@ -146,7 +148,12 @@ impl Config { /// ); /// } /// ``` - pub fn split( + pub fn split< + F: SplitFile + 'static, + R: Keep + 'static, + P: Packer + Sync + 'static, + H: CanRollingPack + 'static, + >( self, file_path: &str, keeper: R, @@ -154,7 +161,13 @@ impl Config { how_pack: H, ) -> Self { self.appends.push(Mutex::new(Box::new( - FileSplitAppender::new::(file_path, Box::new(how_pack), Box::new(keeper), Box::new(packer)).unwrap(), + FileSplitAppender::new::( + file_path, + Box::new(how_pack), + Box::new(keeper), + Box::new(packer), + ) + .expect("new split file fail"), ))); self } diff --git a/src/fast_log.rs b/src/fast_log.rs index b3c5e82..4c15036 100644 --- a/src/fast_log.rs +++ b/src/fast_log.rs @@ -10,7 +10,7 @@ pub static LOGGER: OnceLock = OnceLock::new(); /// get Logger,but you must call `fast_log::init` pub fn logger() -> &'static Logger { - LOGGER.get_or_init(|| { Logger::default() }) + LOGGER.get_or_init(|| Logger::default()) } pub struct Logger { @@ -103,8 +103,14 @@ pub fn init(config: Config) -> Result<&'static Logger, LogError> { return Err(LogError::from("[fast_log] appends can not be empty!")); } let (s, r) = chan(config.chan_len); - logger().send.set(s).map_err(|_| LogError::from("set fail"))?; - logger().recv.set(r).map_err(|_| LogError::from("set fail"))?; + logger() + .send + .set(s) + .map_err(|_| LogError::from("set fail"))?; + logger() + .recv + .set(r) + .map_err(|_| LogError::from("set fail"))?; logger().set_level(config.level); logger() .cfg @@ -112,12 +118,12 @@ pub fn init(config: Config) -> Result<&'static Logger, LogError> { .map_err(|_| LogError::from("set fail="))?; //main recv data log::set_logger(logger()) - .map(|()| log::set_max_level(logger().cfg.get().unwrap().level)) + .map(|()| log::set_max_level(logger().cfg.get().expect("logger cfg is none").level)) .map_err(|e| LogError::from(e))?; let mut receiver_vec = vec![]; let mut sender_vec: Vec>>> = vec![]; - let cfg = logger().cfg.get().unwrap(); + let cfg = logger().cfg.get().expect("logger cfg is none"); for a in cfg.appends.iter() { let (s, r) = chan(cfg.chan_len); sender_vec.push(s); @@ -172,39 +178,47 @@ pub fn init(config: Config) -> Result<&'static Logger, LogError> { let senders = sender_vec.clone(); spawn(move || { loop { - let recv = logger().recv.get().unwrap(); - let mut remain = Vec::with_capacity(recv.len()); - //recv - if recv.len() == 0 { - if let Ok(item) = recv.recv() { - remain.push(item); + if let Some(recv) = logger().recv.get() { + let mut remain = Vec::with_capacity(recv.len()); + //recv + if recv.len() == 0 { + if let Ok(item) = recv.recv() { + remain.push(item); + } } - } - //merge log - loop { - match recv.try_recv() { - Ok(v) => { - remain.push(v); + //merge log + loop { + match recv.try_recv() { + Ok(v) => { + remain.push(v); + } + Err(_) => { + break; + } } - Err(_) => { - break; + } + let mut exit = false; + for x in &mut remain { + if x.formated.is_empty() { + logger() + .cfg + .get() + .expect("logger cfg is none") + .format + .do_format(x); + } + if x.command.eq(&Command::CommandExit) { + exit = true; } } - } - let mut exit = false; - for x in &mut remain { - if x.formated.is_empty() { - logger().cfg.get().unwrap().format.do_format(x); + let data = Arc::new(remain); + for x in senders.iter() { + let _ = x.send(data.clone()); } - if x.command.eq(&Command::CommandExit) { - exit = true; + if exit { + break; } - } - let data = Arc::new(remain); - for x in senders.iter() { - let _ = x.send(data.clone()); - } - if exit { + } else { break; } } diff --git a/src/plugin/file_split.rs b/src/plugin/file_split.rs index 7023529..b2d4b83 100644 --- a/src/plugin/file_split.rs +++ b/src/plugin/file_split.rs @@ -41,7 +41,13 @@ impl Packer for Box { pub trait CanRollingPack: Send { /// is it can do rolling? just return Some(log_file_name).notice the log_file_name must have prefix of temp_name. /// if you return None, it's not do rolling now. - fn can(&mut self, appender: &dyn Packer, temp_name: &str, temp_size: usize, arg: &FastLogRecord) -> Option; + fn can( + &mut self, + appender: &dyn Packer, + temp_name: &str, + temp_size: usize, + arg: &FastLogRecord, + ) -> Option; } /// keep logs, for example keep by log num or keep by log create time. @@ -91,7 +97,6 @@ pub trait SplitFile: Send { fn offset(&self) -> usize; } - ///only use File pub struct RawFile { pub inner: RefCell, @@ -156,7 +161,6 @@ impl SplitFile for RawFile { } } - pub enum DateType { Sec, Hour, @@ -172,7 +176,6 @@ impl Default for DateType { } } - #[allow(dead_code)] pub struct DurationType { last: DateTime, @@ -191,7 +194,6 @@ impl DurationType { } } - pub struct Rolling { last: SystemTime, pub how: RollingType, @@ -214,7 +216,13 @@ pub enum RollingType { } impl CanRollingPack for Rolling { - fn can(&mut self, _appender: &dyn Packer, temp_name: &str, temp_size: usize, arg: &FastLogRecord) -> Option { + fn can( + &mut self, + _appender: &dyn Packer, + temp_name: &str, + temp_size: usize, + arg: &FastLogRecord, + ) -> Option { let last_time = self.last.clone(); self.last = arg.now.clone(); return match &mut self.how { @@ -222,30 +230,21 @@ impl CanRollingPack for Rolling { let last_time = DateTime::from_system_time(last_time, fastdate::offset_sec()); let log_time = DateTime::from_system_time(arg.now, fastdate::offset_sec()); let diff = match date_type { - DateType::Sec => { - log_time.sec() != last_time.sec() - } - DateType::Hour => { - log_time.hour() != last_time.hour() - } - DateType::Minute => { - log_time.minute() != last_time.minute() - } - DateType::Day => { - log_time.day() != last_time.day() - } - DateType::Month => { - log_time.mon() != last_time.mon() - } - DateType::Year => { - log_time.year() != last_time.year() - } + DateType::Sec => log_time.sec() != last_time.sec(), + DateType::Hour => log_time.hour() != last_time.hour(), + DateType::Minute => log_time.minute() != last_time.minute(), + DateType::Day => log_time.day() != last_time.day(), + DateType::Month => log_time.mon() != last_time.mon(), + DateType::Year => log_time.year() != last_time.year(), }; if diff { let log_name = { if let Some(idx) = temp_name.rfind(".") { let suffix = &temp_name[idx..]; - temp_name.replace(suffix, &last_time.format(&format!("YYYY-MM-DDThh-mm-ss.000000{}", suffix))) + temp_name.replace( + suffix, + &last_time.format(&format!("YYYY-MM-DDThh-mm-ss.000000{}", suffix)), + ) } else { let mut temp_name = temp_name.to_string(); temp_name.push_str(&last_time.format("YYYY-MM-DDThh-mm-ss.000000")); @@ -260,10 +259,14 @@ impl CanRollingPack for Rolling { RollingType::BySize(limit) => { if temp_size >= limit.get_len() { let log_name = { - let last_time = DateTime::from_system_time(last_time, fastdate::offset_sec()); + let last_time = + DateTime::from_system_time(last_time, fastdate::offset_sec()); if let Some(idx) = temp_name.rfind(".") { let suffix = &temp_name[idx..]; - temp_name.replace(suffix, &last_time.format(&format!("YYYY-MM-DDThh-mm-ss.000000{}", suffix))) + temp_name.replace( + suffix, + &last_time.format(&format!("YYYY-MM-DDThh-mm-ss.000000{}", suffix)), + ) } else { let mut temp_name = temp_name.to_string(); temp_name.push_str(&last_time.format("YYYY-MM-DDThh-mm-ss.000000")); @@ -284,7 +287,10 @@ impl CanRollingPack for Rolling { let log_name = { if let Some(idx) = temp_name.rfind(".") { let suffix = &temp_name[idx..]; - temp_name.replace(suffix, &last_time.format(&format!("YYYY-MM-DDThh-mm-ss.000000{}", suffix))) + temp_name.replace( + suffix, + &last_time.format(&format!("YYYY-MM-DDThh-mm-ss.000000{}", suffix)), + ) } else { let mut temp_name = temp_name.to_string(); temp_name.push_str(&last_time.format("YYYY-MM-DDThh-mm-ss.000000")); @@ -350,12 +356,7 @@ impl FileSplitAppender { let _ = file.seek(SeekFrom::Start(temp_bytes.load(Ordering::Relaxed) as u64)); let (sender, receiver) = chan(None); let arc_packer = Arc::new(packer); - spawn_saver( - temp_name.clone(), - receiver, - keeper, - arc_packer.clone(), - ); + spawn_saver(temp_name.clone(), receiver, keeper, arc_packer.clone()); Ok(Self { temp_bytes, dir_path: dir_path.to_string(), @@ -412,15 +413,12 @@ impl LogPack { let log_file = OpenOptions::new() .write(true) .read(true) - .open(log_file_path); - if log_file.is_err() { - return Err(LogError::from(format!( - "open(log_file_path={}) fail", - log_file_path - ))); - } + .open(log_file_path) + .map_err(|e| { + LogError::from(format!("open(log_file_path={}) fail={}", log_file_path, e)) + })?; //make - let r = packer.do_pack(log_file.unwrap(), log_file_path); + let r = packer.do_pack(log_file, log_file_path); if r.is_err() && packer.retry() > 0 { let mut retry = 1; while let Err(_packs) = self.do_pack(packer) { @@ -506,7 +504,12 @@ impl LogAppender for FileSplitAppender { let current_temp_size = self.temp_bytes.load(Ordering::Relaxed) + temp.as_bytes().len() + x.formated.as_bytes().len(); - if let Some(new_log_name) = self.can_pack.can(self.packer.deref(), &self.temp_name, current_temp_size, x) { + if let Some(new_log_name) = self.can_pack.can( + self.packer.deref(), + &self.temp_name, + current_temp_size, + x, + ) { self.temp_bytes.fetch_add( { let w = self.file.write(temp.as_bytes()); @@ -526,7 +529,12 @@ impl LogAppender for FileSplitAppender { Command::CommandExit => {} Command::CommandFlush(ref w) => { let current_temp_size = self.temp_bytes.load(Ordering::Relaxed); - if let Some(new_log_name) = self.can_pack.can(self.packer.deref(), &self.temp_name, current_temp_size, x) { + if let Some(new_log_name) = self.can_pack.can( + self.packer.deref(), + &self.temp_name, + current_temp_size, + x, + ) { self.temp_bytes.fetch_add( { let w = self.file.write(temp.as_bytes()); diff --git a/src/plugin/packer.rs b/src/plugin/packer.rs index a2ac37b..e95e98f 100644 --- a/src/plugin/packer.rs +++ b/src/plugin/packer.rs @@ -39,15 +39,8 @@ impl Packer for ZipPacker { log_name = log_name[(v + 1)..log_name.len()].to_string(); } let zip_path = log_file_path.replace(".log", ".zip"); - let zip_file = std::fs::File::create(&zip_path); - if zip_file.is_err() { - return Err(LogError::from(format!( - "[fast_log] create(&{}) fail:{}", - zip_path, - zip_file.err().unwrap() - ))); - } - let zip_file = zip_file.unwrap(); + let zip_file = File::create(&zip_path) + .map_err(|e| LogError::from(format!("[fast_log] create(&{}) fail:{}", zip_path, e)))?; //write zip bytes data let mut zip = zip::ZipWriter::new(zip_file); zip.start_file(log_name, FileOptions::default()) @@ -83,15 +76,8 @@ impl Packer for LZ4Packer { fn do_pack(&self, mut log_file: File, log_file_path: &str) -> Result { let lz4_path = log_file_path.replace(".log", ".lz4"); - let lz4_file = File::create(&lz4_path); - if lz4_file.is_err() { - return Err(LogError::from(format!( - "[fast_log] create(&{}) fail:{}", - lz4_path, - lz4_file.err().unwrap() - ))); - } - let lz4_file = lz4_file.unwrap(); + let lz4_file = File::create(&lz4_path) + .map_err(|e| LogError::from(format!("[fast_log] create(&{}) fail:{}", lz4_path, e)))?; //write lz4 bytes data let mut encoder = FrameEncoder::new(lz4_file); //buf reader @@ -124,15 +110,8 @@ impl Packer for GZipPacker { fn do_pack(&self, mut log_file: File, log_file_path: &str) -> Result { use std::io::Write; let zip_path = log_file_path.replace(".log", ".gz"); - let zip_file = File::create(&zip_path); - if zip_file.is_err() { - return Err(LogError::from(format!( - "[fast_log] create(&{}) fail:{}", - zip_path, - zip_file.err().unwrap() - ))); - } - let zip_file = zip_file.unwrap(); + let zip_file = File::create(&zip_path) + .map_err(|e| LogError::from(format!("[fast_log] create(&{}) fail:{}", zip_path, e)))?; //write zip bytes data let mut zip = GzEncoder::new(zip_file, Compression::default()); std::io::copy(&mut log_file, &mut zip).map_err(|e| LogError::from(e.to_string()))?;