From df90d03c646b94f5a1731a3b4f8a7f47bfebe2d2 Mon Sep 17 00:00:00 2001 From: zxj Date: Sat, 27 Jul 2024 20:12:46 +0800 Subject: [PATCH] add CanRollingPack --- README.md | 35 ++++++++++++++--------------- example/src/split_log.rs | 5 ++--- example/src/split_log_date.rs | 6 ++--- example/src/split_log_duration.rs | 5 ++--- example/src/split_log_gz.rs | 4 ++-- example/src/split_log_loop.rs | 1 - example/src/split_log_lz4.rs | 4 ++-- example/src/split_log_zip.rs | 4 ++-- src/config.rs | 24 ++++++++++---------- src/plugin/file_loop.rs | 4 ++-- src/plugin/file_split.rs | 37 ++++++++++++++----------------- tests/split_test.rs | 4 ++-- 12 files changed, 62 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index 1972856..97d8100 100644 --- a/README.md +++ b/README.md @@ -134,19 +134,18 @@ fn main(){ ```rust use fast_log::config::Config; -use fast_log::plugin::file_split::{PackType, KeepType, DateType, HowPack}; +use fast_log::plugin::file_split::{RollingType, KeepType, DateType, Rolling}; use std::thread::sleep; use std::time::Duration; use fast_log::plugin::packer::LogPacker; - fn main() { - //file_path also can use '"target/logs/test.log"' fast_log::init(Config::new().chan_len(Some(100000)).console().file_split( "target/logs/", - HowPack::new(PackType::ByDate(DateType::Day)), + Rolling::new(RollingType::ByDate(DateType::Day)), KeepType::KeepNum(2), LogPacker {}, - )).unwrap(); + )) + .unwrap(); for _ in 0..60 { sleep(Duration::from_secs(1)); log::info!("Commencing yak shaving"); @@ -163,23 +162,23 @@ fn main() { ```rust use fast_log::config::Config; use fast_log::consts::LogSize; +use fast_log::plugin::file_split::{RollingType, KeepType, Rolling}; use fast_log::plugin::packer::LogPacker; -use fast_log::plugin::file_split::{PackType, KeepType, HowPack}; - -#[test] -pub fn test_file_compation() { - fast_log::init(Config::new() - .console() - .chan_len(Some(100000)) - .file_split("target/logs/", - HowPack::new(PackType::BySize(LogSize::MB(1))), - KeepType::All, - LogPacker{})).unwrap(); - for _ in 0..200000 { - info!("Commencing yak shaving"); +fn main() { + fast_log::init(Config::new().chan_len(Some(100000)).console().file_split( + "target/logs/", + Rolling::new(RollingType::BySize(LogSize::KB(500))), + KeepType::KeepNum(2), + LogPacker {}, + )) + .unwrap(); + for _ in 0..40000 { + log::info!("Commencing yak shaving"); } log::logger().flush(); + println!("you can see log files in path: {}", "target/logs/") } + ``` diff --git a/example/src/split_log.rs b/example/src/split_log.rs index 2a1383b..b8ed8bf 100644 --- a/example/src/split_log.rs +++ b/example/src/split_log.rs @@ -1,13 +1,12 @@ use fast_log::config::Config; use fast_log::consts::LogSize; -use fast_log::plugin::file_split::{PackType, KeepType, HowPack}; +use fast_log::plugin::file_split::{RollingType, KeepType, Rolling}; use fast_log::plugin::packer::LogPacker; fn main() { - //file_path also can use '"target/logs/test.log"' fast_log::init(Config::new().chan_len(Some(100000)).console().file_split( "target/logs/", - HowPack::new(PackType::BySize(LogSize::KB(500))), + Rolling::new(RollingType::BySize(LogSize::KB(500))), KeepType::KeepNum(2), LogPacker {}, )) diff --git a/example/src/split_log_date.rs b/example/src/split_log_date.rs index d81762e..c86c2ed 100644 --- a/example/src/split_log_date.rs +++ b/example/src/split_log_date.rs @@ -1,15 +1,13 @@ use fast_log::config::Config; -use fast_log::plugin::file_split::{PackType, KeepType, DateType, HowPack}; +use fast_log::plugin::file_split::{RollingType, KeepType, DateType, Rolling}; use std::thread::sleep; use std::time::Duration; use fast_log::plugin::packer::LogPacker; - fn main() { - //file_path also can use '"target/logs/test.log"' fast_log::init(Config::new().chan_len(Some(100000)).console().file_split( "target/logs/", - HowPack::new(PackType::ByDate(DateType::Day)), + Rolling::new(RollingType::ByDate(DateType::Day)), KeepType::KeepNum(2), LogPacker {}, )) diff --git a/example/src/split_log_duration.rs b/example/src/split_log_duration.rs index 4049911..884f37f 100644 --- a/example/src/split_log_duration.rs +++ b/example/src/split_log_duration.rs @@ -1,5 +1,5 @@ use fast_log::config::Config; -use fast_log::plugin::file_split::{PackType, KeepType, HowPack}; +use fast_log::plugin::file_split::{RollingType, KeepType, Rolling}; use std::thread::sleep; use std::time::Duration; use fastdate::DateTime; @@ -7,10 +7,9 @@ use fast_log::plugin::packer::LogPacker; fn main() { - //file_path also can use '"target/logs/test.log"' fast_log::init(Config::new().chan_len(Some(100000)).console().file_split( "target/logs/", - HowPack::new(PackType::ByDuration((DateTime::now(), Duration::from_secs(5)))), + Rolling::new(RollingType::ByDuration((DateTime::now(), Duration::from_secs(5)))), KeepType::KeepNum(5), LogPacker {}, )) diff --git a/example/src/split_log_gz.rs b/example/src/split_log_gz.rs index 41c12ef..1427d3e 100644 --- a/example/src/split_log_gz.rs +++ b/example/src/split_log_gz.rs @@ -1,12 +1,12 @@ use fast_log::config::Config; use fast_log::consts::LogSize; -use fast_log::plugin::file_split::{PackType, KeepType, HowPack}; +use fast_log::plugin::file_split::{RollingType, KeepType, Rolling}; use fast_log::plugin::packer::GZipPacker; fn main() { fast_log::init(Config::new().chan_len(Some(100000)).console().file_split( "target/logs/", - HowPack::new(PackType::BySize(LogSize::KB(50))), + Rolling::new(RollingType::BySize(LogSize::KB(50))), KeepType::KeepNum(5), GZipPacker {}, )) diff --git a/example/src/split_log_loop.rs b/example/src/split_log_loop.rs index 42fbc8d..c99baa3 100644 --- a/example/src/split_log_loop.rs +++ b/example/src/split_log_loop.rs @@ -3,7 +3,6 @@ use fast_log::consts::LogSize; ///Single logs are stored in rolling mode by capacity fn main() { - //or: let file_name = "sloop.log" let file_name = "target/logs/sloop.log"; fast_log::init( Config::new() diff --git a/example/src/split_log_lz4.rs b/example/src/split_log_lz4.rs index d008521..9819558 100644 --- a/example/src/split_log_lz4.rs +++ b/example/src/split_log_lz4.rs @@ -1,12 +1,12 @@ use fast_log::config::Config; use fast_log::consts::LogSize; -use fast_log::plugin::file_split::{PackType, KeepType, HowPack}; +use fast_log::plugin::file_split::{RollingType, KeepType, Rolling}; use fast_log::plugin::packer::LZ4Packer; fn main() { fast_log::init(Config::new().chan_len(Some(100000)).console().file_split( "target/logs/", - HowPack::new(PackType::BySize(LogSize::KB(50))), + Rolling::new(RollingType::BySize(LogSize::KB(50))), KeepType::KeepNum(5), LZ4Packer {}, )) diff --git a/example/src/split_log_zip.rs b/example/src/split_log_zip.rs index 450e1b1..01cacd7 100644 --- a/example/src/split_log_zip.rs +++ b/example/src/split_log_zip.rs @@ -1,5 +1,5 @@ use fast_log::consts::LogSize; -use fast_log::plugin::file_split::{PackType, KeepType, HowPack}; +use fast_log::plugin::file_split::{RollingType, KeepType, Rolling}; use fast_log::plugin::packer::ZipPacker; use fast_log::config::Config; @@ -7,7 +7,7 @@ use fast_log::config::Config; fn main() { fast_log::init(Config::new().chan_len(Some(100000)).console().file_split( "target/logs/", - HowPack::new(PackType::BySize(LogSize::KB(50))), + Rolling::new(RollingType::BySize(LogSize::KB(50))), KeepType::KeepNum(5), ZipPacker {}, )) diff --git a/src/config.rs b/src/config.rs index 427c296..2dc6bfe 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,7 +4,7 @@ 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, CanPack, Keep, Packer, RawFile, SplitFile}; +use crate::plugin::file_split::{FileSplitAppender, CanRollingPack, Keep, Packer, RawFile, SplitFile}; use crate::FastLogFormat; use dark_std::sync::SyncVec; use log::LevelFilter; @@ -102,21 +102,21 @@ impl Config { self } /// add a FileSplitAppender - pub fn file_split( + pub fn file_split( self, file_path: &str, - how:H, - rolling_type: R, - packer: P, + rolling: Rolling, + keeper: Keeper, + packer: Packer, ) -> Self { self.appends.push(Mutex::new(Box::new( FileSplitAppender::new::( file_path, - Box::new(how), - Box::new(rolling_type), + Box::new(rolling), + Box::new(keeper), Box::new(packer), ) - .unwrap(), + .unwrap(), ))); self } @@ -127,7 +127,7 @@ impl Config { /// ```rust /// use fast_log::Config; /// use fast_log::consts::LogSize; - /// use fast_log::plugin::file_split::{HowPack, PackType, RawFile, RollingType}; + /// use fast_log::plugin::file_split::{Rolling, RollingType, RawFile, RollingType}; /// use fast_log::plugin::packer::LogPacker; /// fn new(){ /// fast_log::init( @@ -137,17 +137,17 @@ impl Config { /// "target/logs/temp.log", /// RollingType::All, /// LogPacker {}, - /// HowPack::new(PackType::BySize(LogSize::MB(1))), + /// Rolling::new(RollingType::BySize(LogSize::MB(1))), /// ), /// ); /// } /// ``` - pub fn split( + pub fn split( self, file_path: &str, keeper: R, packer: P, - how_pack:H, + 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(), diff --git a/src/plugin/file_loop.rs b/src/plugin/file_loop.rs index 8f6e2e2..e085d05 100644 --- a/src/plugin/file_loop.rs +++ b/src/plugin/file_loop.rs @@ -1,7 +1,7 @@ use crate::appender::{FastLogRecord, LogAppender}; use crate::consts::LogSize; use crate::error::LogError; -use crate::plugin::file_split::{FileSplitAppender, PackType, KeepType, RawFile, HowPack}; +use crate::plugin::file_split::{FileSplitAppender, RollingType, KeepType, RawFile, Rolling}; use crate::plugin::packer::LogPacker; /// Single logs are stored in rolling mode by capacity @@ -14,7 +14,7 @@ impl FileLoopAppender { Ok(Self { file: FileSplitAppender::new::( log_file_path, - Box::new(HowPack::new(PackType::BySize(size))), + Box::new(Rolling::new(RollingType::BySize(size))), Box::new(KeepType::KeepNum(1)), Box::new(LogPacker {}), )?, diff --git a/src/plugin/file_split.rs b/src/plugin/file_split.rs index c69ff2a..57e5007 100644 --- a/src/plugin/file_split.rs +++ b/src/plugin/file_split.rs @@ -38,8 +38,8 @@ impl Packer for Box { } /// is can do pack? -pub trait CanPack: Send { - fn is_pack(&mut self, appender: &dyn Packer, temp_name: &str, temp_size: usize, arg: &FastLogRecord) -> Option; +pub trait CanRollingPack: Send { + 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. @@ -190,13 +190,13 @@ impl DurationType { } -pub struct HowPack { +pub struct Rolling { last: SystemTime, - pub how: PackType, + pub how: RollingType, } -impl HowPack { - pub fn new(how: PackType) -> Self { +impl Rolling { + pub fn new(how: RollingType) -> Self { Self { last: SystemTime::now(), how: how, @@ -204,18 +204,19 @@ impl HowPack { } } -pub enum PackType { +///log rolling type +pub enum RollingType { ByDate(DateType), BySize(LogSize), ByDuration((DateTime, Duration)), } -impl CanPack for HowPack { - fn is_pack(&mut self, _appender: &dyn Packer, temp_name: &str, temp_size: usize, arg: &FastLogRecord) -> Option { +impl CanRollingPack for Rolling { + 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 { - PackType::ByDate(date_type) => { + RollingType::ByDate(date_type) => { 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 { @@ -254,7 +255,7 @@ impl CanPack for HowPack { None } } - PackType::BySize(limit) => { + RollingType::BySize(limit) => { if temp_size >= limit.get_len() { let log_name = { let last_time = DateTime::from_system_time(last_time, fastdate::offset_sec()); @@ -272,7 +273,7 @@ impl CanPack for HowPack { None } } - PackType::ByDuration((start_time, duration)) => { + RollingType::ByDuration((start_time, duration)) => { let log_time = DateTime::from_system_time(arg.now, fastdate::offset_sec()); let next = start_time.clone().add(duration.clone()); if log_time >= next { @@ -305,7 +306,7 @@ pub struct FileSplitAppender { packer: Arc>, dir_path: String, sender: Sender, - can_pack: Box, + can_pack: Box, //cache data temp_bytes: AtomicUsize, temp_name: String, @@ -314,7 +315,7 @@ pub struct FileSplitAppender { impl FileSplitAppender { pub fn new( file_path: &str, - how_to_pack: Box, + how_to_pack: Box, rolling_type: Box, packer: Box, ) -> Result { @@ -434,10 +435,6 @@ impl LogPack { } } - -///rolling keep type -pub type RollingType = KeepType; - ///rolling keep type #[derive(Copy, Clone, Debug)] pub enum KeepType { @@ -499,7 +496,7 @@ 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.is_pack(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()); @@ -519,7 +516,7 @@ 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.is_pack(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/tests/split_test.rs b/tests/split_test.rs index 10545c8..c679d3d 100644 --- a/tests/split_test.rs +++ b/tests/split_test.rs @@ -3,7 +3,7 @@ mod test { use fast_log::appender::{Command, FastLogRecord, LogAppender}; use fast_log::consts::LogSize; use fast_log::plugin::file_name::FileName; - use fast_log::plugin::file_split::{FileSplitAppender, PackType, Keep, RawFile, RollingType, HowPack}; + use fast_log::plugin::file_split::{FileSplitAppender, RollingType, Keep, RawFile, RollingType, Rolling}; use fast_log::plugin::packer::LogPacker; use fastdate::DateTime; use log::Level; @@ -16,7 +16,7 @@ mod test { let _ = remove_dir_all("target/test/"); let mut appender = FileSplitAppender::new::( "target/test/", - Box::new(HowPack::new(PackType::BySize(LogSize::MB(1)))), + Box::new(Rolling::new(RollingType::BySize(LogSize::MB(1)))), Box::new(RollingType::All), Box::new(LogPacker {}), )