Skip to content

Commit

Permalink
add impl CanPack
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Jul 28, 2024
1 parent fd7cee2 commit 0943894
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 12 deletions.
3 changes: 3 additions & 0 deletions example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ path = "src/format_log_json.rs"
[[bin]]
name = "split_log_date"
path = "src/split_log_date.rs"
[[bin]]
name = "split_log_duration"
path = "src/split_log_duration.rs"
[dependencies]
log = { version = "0.4", features = ["std"] }
crossbeam-channel = "0.5"
Expand Down
4 changes: 2 additions & 2 deletions example/src/split_log_date.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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};
use fast_log::plugin::file_split::{PackType, KeepType, Packer, DateType, DiffDateType};
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use std::thread::sleep;
Expand All @@ -14,7 +14,7 @@ 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(DateTime::now()),
PackType::ByDate(DateType::new(DiffDateType::Day)),
KeepType::KeepNum(2),
LogPacker {},
))
Expand Down
24 changes: 24 additions & 0 deletions example/src/split_log_duration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use fast_log::config::Config;
use fast_log::plugin::file_split::{PackType, KeepType};
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((DateTime::now(),Duration::from_secs(5))),
KeepType::KeepNum(5),
LogPacker {},
))
.unwrap();
for _ in 0..60 {
sleep(Duration::from_secs(1));
log::info!("Commencing yak shaving");
}
log::logger().flush();
println!("you can see log files in path: {}", "target/logs/")
}
82 changes: 72 additions & 10 deletions src/plugin/file_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,33 +155,95 @@ impl SplitFile for RawFile {
}


pub enum DiffDateType {
Sec,
Hour,
Minute,
Day,
Month,
Year,
}

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

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

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

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

impl CanPack for PackType {
fn is_pack(&mut self, _appender: &dyn Packer, temp_name: &str, temp_size: usize, arg: &FastLogRecord) -> Option<String> {
return match self {
PackType::ByDate(date_time) => {
PackType::ByDate(date_type) => {
let dt = DateTime::from_system_time(arg.now, fastdate::offset_sec());
if dt.day() > date_time.day() {
let last_time = date_time.clone();
*date_time = dt;
Some(temp_name.replace(".log",&last_time.format("YYYY-MM-DDThh-mm-ss.000000.log")))
let diff = match date_type.diff {
DiffDateType::Sec => {
dt.sec() != date_type.inner.sec()
}
DiffDateType::Hour => {
dt.hour() != date_type.inner.hour()
}
DiffDateType::Minute => {
dt.minute() != date_type.inner.minute()
}
DiffDateType::Day => {
dt.day() != date_type.inner.day()
}
DiffDateType::Month => {
dt.mon() != date_type.inner.mon()
}
DiffDateType::Year => {
dt.year() != date_type.inner.year()
}
};
if diff {
let log_name = temp_name.replace(".log", &date_type.inner.format("YYYY-MM-DDThh-mm-ss.000000.log"));
date_type.inner = dt;
Some(log_name)
} else {
None
}
}
PackType::BySize(limit) => {
if temp_size >= limit.get_len() {
Some(temp_name.replace(".log",&DateTime::now().format("YYYY-MM-DDThh-mm-ss.000000.log")))
Some(temp_name.replace(".log", &DateTime::now().format("YYYY-MM-DDThh-mm-ss.000000.log")))
} else {
None
}
}
PackType::ByDuration((start_time, d)) => {
let dt = DateTime::from_system_time(arg.now, fastdate::offset_sec());
let next = start_time.clone().add(d.clone());
if dt >= next {
let now = DateTime::now();
let log_name = temp_name.replace(".log", &now.format("YYYY-MM-DDThh-mm-ss.000000.log"));
*start_time = now;
Some(log_name)
} else {
None
}
}
PackType::ByCanPack(c) => {
PackType::ByCustom(c) => {
c.is_pack(_appender, temp_name, temp_size, arg)
}
};
Expand Down Expand Up @@ -277,7 +339,7 @@ impl FileSplitAppender {
self.temp_bytes.store(0, Ordering::SeqCst);
}

pub fn temp_name(&self) ->&str{
pub fn temp_name(&self) -> &str {
&self.temp_name
}
}
Expand Down

0 comments on commit 0943894

Please sign in to comment.