From 611ef0f5a2afba965f12b10201e6b810105eb5fc Mon Sep 17 00:00:00 2001 From: zxj Date: Tue, 19 Sep 2023 16:31:54 +0800 Subject: [PATCH] add trait Rolling --- example/src/split_log.rs | 4 +-- src/plugin/file_loop.rs | 3 ++- src/plugin/file_split.rs | 53 ---------------------------------------- src/plugin/mod.rs | 2 ++ src/plugin/rolling.rs | 53 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 56 deletions(-) create mode 100644 src/plugin/rolling.rs diff --git a/example/src/split_log.rs b/example/src/split_log.rs index a4d9f03..5d51c29 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::file_split::RollingType; use fast_log::plugin::packer::LogPacker; +use fast_log::plugin::rolling::RollingAll; 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), - RollingType::All, + RollingAll {}, LogPacker {}, )) .unwrap(); diff --git a/src/plugin/file_loop.rs b/src/plugin/file_loop.rs index 3054d06..4f62b52 100644 --- a/src/plugin/file_loop.rs +++ b/src/plugin/file_loop.rs @@ -1,8 +1,9 @@ use crate::appender::{FastLogRecord, LogAppender}; use crate::consts::LogSize; use crate::error::LogError; -use crate::plugin::file_split::{FileSplitAppender, RollingNum, SplitFile}; +use crate::plugin::file_split::{FileSplitAppender, SplitFile}; use crate::plugin::packer::LogPacker; +use crate::plugin::rolling::RollingNum; /// Single logs are stored in rolling mode by capacity pub struct FileLoopAppender { diff --git a/src/plugin/file_split.rs b/src/plugin/file_split.rs index 2395b24..233715a 100644 --- a/src/plugin/file_split.rs +++ b/src/plugin/file_split.rs @@ -311,69 +311,16 @@ pub trait Rolling: Send { #[derive(Copy, Clone, Debug)] pub enum RollingType { /// keep All of log packs - #[deprecated(note = "use RollingAll,RollingNum,RollingTime replace this")] All, /// keep by Time Duration, /// for example: /// // keep one day log pack /// (Duration::from_secs(24 * 3600)) - #[deprecated(note = "use RollingAll,RollingNum,RollingTime replace this")] KeepTime(Duration), /// keep log pack num(.log,.zip.lz4...more) - #[deprecated(note = "use RollingAll,RollingNum,RollingTime replace this")] KeepNum(i64), } -pub struct RollingAll {} -impl Rolling for RollingAll { - fn do_rolling(&self, dir: &str, temp_name: &str) -> i64 { - 0 - } -} - -pub struct RollingNum { - pub num: 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 { - let item = &paths_vec[index]; - std::fs::remove_file(item.path()); - removed += 1; - } - } - removed - } -} - -pub struct RollingTime { - pub duration: Duration, -} - -impl Rolling for RollingTime { - fn do_rolling(&self, dir: &str, temp_name: &str) -> i64 { - let mut removed = 0; - let paths_vec = self.read_paths(dir, temp_name); - let now = DateTime::now(); - for index in 0..paths_vec.len() { - let item = &paths_vec[index]; - let file_name = item.file_name(); - let name = file_name.to_str().unwrap_or("").to_string(); - if let Some(time) = Self::file_name_parse_time(&name, temp_name) { - if now.clone().sub(self.duration.clone()) > time { - std::fs::remove_file(item.path()); - removed += 1; - } - } - } - removed - } -} - impl Rolling for RollingType { fn do_rolling(&self, temp_name: &str, dir: &str) -> i64 { let mut removed = 0; diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index cdb2f71..810d142 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -5,3 +5,5 @@ pub mod file_loop; pub mod file_mmap; pub mod file_split; pub mod packer; + +pub mod rolling; diff --git a/src/plugin/rolling.rs b/src/plugin/rolling.rs new file mode 100644 index 0000000..d7a9d4e --- /dev/null +++ b/src/plugin/rolling.rs @@ -0,0 +1,53 @@ +use crate::plugin::file_split::Rolling; +use fastdate::DateTime; +use std::time::Duration; + +pub struct RollingAll {} +impl Rolling for RollingAll { + fn do_rolling(&self, dir: &str, temp_name: &str) -> i64 { + 0 + } +} + +pub struct RollingNum { + pub num: 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 { + let item = &paths_vec[index]; + std::fs::remove_file(item.path()); + removed += 1; + } + } + removed + } +} + +pub struct RollingTime { + pub duration: Duration, +} + +impl Rolling for RollingTime { + fn do_rolling(&self, dir: &str, temp_name: &str) -> i64 { + let mut removed = 0; + let paths_vec = self.read_paths(dir, temp_name); + let now = DateTime::now(); + for index in 0..paths_vec.len() { + let item = &paths_vec[index]; + let file_name = item.file_name(); + let name = file_name.to_str().unwrap_or("").to_string(); + if let Some(time) = Self::file_name_parse_time(&name, temp_name) { + if now.clone().sub(self.duration.clone()) > time { + std::fs::remove_file(item.path()); + removed += 1; + } + } + } + removed + } +}