Skip to content

Commit

Permalink
add HowPack
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Jul 27, 2024
1 parent 4d49e78 commit 2eeb319
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 56 deletions.
9 changes: 2 additions & 7 deletions example/src/split_log_date.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
use fast_log::config::Config;
use fast_log::error::LogError;
use fast_log::plugin::file_name::FileName;
use fast_log::plugin::file_split::{PackType, KeepType, Packer, DateType, DiffDateType};
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use fast_log::plugin::file_split::{PackType, KeepType, DateType, HowPack};
use std::thread::sleep;
use std::time::Duration;
use fastdate::DateTime;
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/",
PackType::ByDate(DateType::new(DiffDateType::Day)),
HowPack::new(PackType::ByDate(DateType::Day)),
KeepType::KeepNum(2),
LogPacker {},
))
Expand Down
5 changes: 3 additions & 2 deletions example/src/split_log_duration.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use fast_log::config::Config;
use fast_log::plugin::file_split::{PackType, KeepType, DurationType};
use fast_log::plugin::file_split::{PackType, KeepType, DurationType, HowPack};
use std::thread::sleep;
use std::time::Duration;
use fastdate::DateTime;
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/",
PackType::ByDuration(DurationType::new(Duration::from_secs(5))),
HowPack::new(PackType::ByDuration((DateTime::now(), Duration::from_secs(5)))),
KeepType::KeepNum(5),
LogPacker {},
))
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/file_loop.rs
Original file line number Diff line number Diff line change
@@ -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};
use crate::plugin::file_split::{FileSplitAppender, PackType, KeepType, RawFile, HowPack};
use crate::plugin::packer::LogPacker;

/// Single logs are stored in rolling mode by capacity
Expand All @@ -14,7 +14,7 @@ impl FileLoopAppender {
Ok(Self {
file: FileSplitAppender::new::<RawFile>(
log_file_path,
Box::new(PackType::BySize(size)),
Box::new(HowPack::new(PackType::BySize(size))),
Box::new(KeepType::KeepNum(1)),
Box::new(LogPacker {}),
)?,
Expand Down
87 changes: 42 additions & 45 deletions src/plugin/file_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl SplitFile for RawFile {
}


pub enum DiffDateType {
pub enum DateType {
Sec,
Hour,
Minute,
Expand All @@ -164,23 +164,9 @@ pub enum DiffDateType {
Year,
}

pub struct DateType {
last: DateTime,
pub diff: DiffDateType,
}

impl DateType {
pub fn new(arg: DiffDateType) -> Self {
Self {
last: DateTime::now(),
diff: arg,
}
}
}

impl Default for DateType {
fn default() -> Self {
Self::new(DiffDateType::Day)
Self::Day
}
}

Expand All @@ -202,44 +188,58 @@ impl DurationType {
}
}


pub struct HowPack {
last: DateTime,
pub how: PackType,
}

impl HowPack {
pub fn new(how:PackType)->Self{
Self{
last: DateTime::now(),
how:how,
}
}
}

pub enum PackType {
ByDate(DateType),
BySize(LogSize),
ByDuration(DurationType),
ByCustom(Box<dyn CanPack>),
ByDuration((DateTime, Duration)),
}

impl CanPack for PackType {
impl CanPack for HowPack {
fn is_pack(&mut self, _appender: &dyn Packer, temp_name: &str, temp_size: usize, arg: &FastLogRecord) -> Option<String> {
return match self {
return match &mut self.how {
PackType::ByDate(date_type) => {
let dt = DateTime::from_system_time(arg.now, fastdate::offset_sec());
let diff = match date_type.diff {
DiffDateType::Sec => {
dt.sec() != date_type.last.sec()
let diff = match date_type {
DateType::Sec => {
dt.sec() != self.last.sec()
}
DiffDateType::Hour => {
dt.hour() != date_type.last.hour()
DateType::Hour => {
dt.hour() != self.last.hour()
}
DiffDateType::Minute => {
dt.minute() != date_type.last.minute()
DateType::Minute => {
dt.minute() != self.last.minute()
}
DiffDateType::Day => {
dt.day() != date_type.last.day()
DateType::Day => {
dt.day() != self.last.day()
}
DiffDateType::Month => {
dt.mon() != date_type.last.mon()
DateType::Month => {
dt.mon() != self.last.mon()
}
DiffDateType::Year => {
dt.year() != date_type.last.year()
DateType::Year => {
dt.year() != self.last.year()
}
};
if diff {
let log_name = temp_name.replace(".log", &date_type.last.format("YYYY-MM-DDThh-mm-ss.000000.log"));
date_type.last = dt;
let log_name = temp_name.replace(".log", &self.last.format("YYYY-MM-DDThh-mm-ss.000000.log"));
self.last = dt;
Some(log_name)
} else {
date_type.last = dt;
self.last = dt;
None
}
}
Expand All @@ -250,23 +250,20 @@ impl CanPack for PackType {
None
}
}
PackType::ByDuration(duration_type) => {
PackType::ByDuration((start_time, duration)) => {
let dt = DateTime::from_system_time(arg.now, fastdate::offset_sec());
let next = duration_type.start_time.clone().add(duration_type.duration.clone());
let next = start_time.clone().add(duration.clone());
if dt >= next {
let now = DateTime::now();
let log_name = temp_name.replace(".log", &duration_type.last.format("YYYY-MM-DDThh-mm-ss.000000.log"));
duration_type.start_time = now;
duration_type.last = dt;
let log_name = temp_name.replace(".log", &self.last.format("YYYY-MM-DDThh-mm-ss.000000.log"));
*start_time = now;
self.last = dt;
Some(log_name)
} else {
duration_type.last = dt;
self.last = dt;
None
}
}
PackType::ByCustom(c) => {
c.is_pack(_appender, temp_name, temp_size, arg)
}
};
}
}
Expand Down

0 comments on commit 2eeb319

Please sign in to comment.