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 b8efadc commit 611ef0f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 56 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::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();
Expand Down
3 changes: 2 additions & 1 deletion src/plugin/file_loop.rs
Original file line number Diff line number Diff line change
@@ -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<F: SplitFile> {
Expand Down
53 changes: 0 additions & 53 deletions src/plugin/file_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ pub mod file_loop;
pub mod file_mmap;
pub mod file_split;
pub mod packer;

pub mod rolling;
53 changes: 53 additions & 0 deletions src/plugin/rolling.rs
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 611ef0f

Please sign in to comment.