From 95ee286216914c1fdce7168452a8641cd2a0c138 Mon Sep 17 00:00:00 2001 From: zxj Date: Tue, 19 Sep 2023 17:22:12 +0800 Subject: [PATCH] add trait Rolling --- example/src/split_log.rs | 4 ++-- src/plugin/file_loop.rs | 2 +- src/plugin/rolling.rs | 12 ++++-------- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/example/src/split_log.rs b/example/src/split_log.rs index 5d51c29..9cd8f0d 100644 --- a/example/src/split_log.rs +++ b/example/src/split_log.rs @@ -1,14 +1,14 @@ use fast_log::config::Config; use fast_log::consts::LogSize; use fast_log::plugin::packer::LogPacker; -use fast_log::plugin::rolling::RollingAll; +use fast_log::plugin::rolling::{RollingAll, RollingNum}; 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/", LogSize::MB(1), - RollingAll {}, + RollingNum(2), LogPacker {}, )) .unwrap(); diff --git a/src/plugin/file_loop.rs b/src/plugin/file_loop.rs index 6ec7aca..4294694 100644 --- a/src/plugin/file_loop.rs +++ b/src/plugin/file_loop.rs @@ -16,7 +16,7 @@ impl FileLoopAppender { file: FileSplitAppender::::new( log_file_path, size, - RollingNum { num: 1 }, + RollingNum(1), LogPacker {}, )?, }) diff --git a/src/plugin/rolling.rs b/src/plugin/rolling.rs index b86a267..829aafe 100644 --- a/src/plugin/rolling.rs +++ b/src/plugin/rolling.rs @@ -11,16 +11,14 @@ impl Rolling for RollingAll { } /// rolling from file num -pub struct RollingNum { - pub num: i64, -} +pub struct RollingNum(pub i64); impl Rolling for RollingNum { fn do_rolling(&self, dir: &str, temp_name: &str) -> i64 { let mut removed = 0; let paths_vec = self.read_paths(dir, temp_name); for index in 0..paths_vec.len() { - if index >= (self.num) as usize { + if index >= (self.0) as usize { let item = &paths_vec[index]; std::fs::remove_file(item.path()); removed += 1; @@ -31,9 +29,7 @@ impl Rolling for RollingNum { } /// rolling from metadata -pub struct RollingDuration { - pub duration: Duration, -} +pub struct RollingDuration(pub Duration); impl Rolling for RollingDuration { fn do_rolling(&self, dir: &str, temp_name: &str) -> i64 { @@ -47,7 +43,7 @@ impl Rolling for RollingDuration { if let Ok(m) = item.metadata() { if let Ok(c) = m.created() { let time = DateTime::from(c); - if now.clone().sub(self.duration.clone()) > time { + if now.clone().sub(self.0.clone()) > time { std::fs::remove_file(item.path()); removed += 1; }