diff --git a/Cargo.toml b/Cargo.toml index f2b32ec..266b127 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ gzip = ["flate2"] runtime_thread = [] [dependencies] -chrono = { version = "0.4", features = ["serde"] } +fastdate = "0.1" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" log = { version = "0.4", features = ["std"] } diff --git a/src/appender.rs b/src/appender.rs index 051d799..561f035 100644 --- a/src/appender.rs +++ b/src/appender.rs @@ -60,8 +60,6 @@ pub trait RecordFormat: Send + Sync { } pub struct FastLogFormat { - // Time zone Interval hour - pub duration_zone: Duration, // show line level pub display_line_level: log::LevelFilter, } @@ -71,7 +69,7 @@ impl RecordFormat for FastLogFormat { match arg.command { CommandRecord => { let data; - let now = date::LogDate::from(arg.now.add(self.duration_zone)); + let now = fastdate::DateTime::now(); if arg.level.to_level_filter() <= self.display_line_level { data = format!( "{:29} {} {} - {} {}:{}\n", @@ -98,15 +96,8 @@ impl RecordFormat for FastLogFormat { } impl FastLogFormat { - pub fn local_duration() -> Duration { - let utc = chrono::Utc::now().naive_utc(); - let tz = chrono::Local::now().naive_local(); - tz.sub(utc).to_std().unwrap_or_default() - } - pub fn new() -> FastLogFormat { Self { - duration_zone: Self::local_duration(), display_line_level: LevelFilter::Warn, } } @@ -116,10 +107,4 @@ impl FastLogFormat { self.display_line_level = level; self } - - /// Time zone Interval hour - pub fn set_duration(mut self, duration: Duration) -> Self { - self.duration_zone = duration; - self - } } diff --git a/src/plugin/file_split.rs b/src/plugin/file_split.rs index 405d91a..0531d4f 100644 --- a/src/plugin/file_split.rs +++ b/src/plugin/file_split.rs @@ -2,13 +2,12 @@ use std::cell::RefCell; use std::fs::{DirEntry, File, OpenOptions}; use std::io::{Seek, SeekFrom, Write}; -use chrono::{Local, NaiveDateTime}; - use crate::appender::{Command, FastLogRecord, LogAppender}; use crate::consts::LogSize; use crate::error::LogError; use crate::{chan, Receiver, Sender}; use std::ops::Sub; +use std::str::FromStr; use std::time::Duration; /// .zip or .lz4 or any one packer @@ -87,20 +86,15 @@ impl RollingType { } } } - RollingType::KeepTime(t) => { + RollingType::KeepTime(duration) => { let paths_vec = self.read_paths(dir, temp_name); - let duration = chrono::Duration::from_std(t.clone()); - if duration.is_err() { - return; - } - let duration = duration.unwrap(); - let now = Local::now().naive_local(); + let now = fastdate::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.sub(time) > duration { + if now.sub(duration.clone()) > time { std::fs::remove_file(item.path()); } } @@ -110,13 +104,13 @@ impl RollingType { } } - fn file_name_parse_time(&self, name: &str, temp_name: &str) -> Option { + fn file_name_parse_time(&self, name: &str, temp_name: &str) -> Option { if name.starts_with(temp_name) { let mut time_str = name.replace(temp_name, ""); if let Some(v) = time_str.find(".") { time_str = time_str[0..v].to_string(); } - let time = chrono::NaiveDateTime::parse_from_str(&time_str, "%Y_%m_%dT%H_%M_%S"); + let time = fastdate::DateTime::from_str(&time_str); if let Ok(time) = time { return Some(time); } @@ -146,7 +140,7 @@ impl FileSplitAppenderData { "{}{}{}.log", self.dir_path, &self.temp_name, - format!("{:29}", Local::now().format("%Y_%m_%dT%H_%M_%S%.f")).replace(" ", "_") + format!("{:29}", fastdate::DateTime::now()) ); std::fs::copy(&first_file_path, &new_log_name); self.sender.send(LogPack {