Skip to content

Commit

Permalink
add trait Rolling
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Sep 19, 2023
1 parent dbf9e5b commit 95ee286
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
4 changes: 2 additions & 2 deletions example/src/split_log.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/file_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl<F: SplitFile> FileLoopAppender<F> {
file: FileSplitAppender::<F, LogPacker>::new(
log_file_path,
size,
RollingNum { num: 1 },
RollingNum(1),
LogPacker {},
)?,
})
Expand Down
12 changes: 4 additions & 8 deletions src/plugin/rolling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
}
Expand Down

0 comments on commit 95ee286

Please sign in to comment.