Skip to content

Commit

Permalink
use fastdate
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Jul 19, 2022
1 parent 9c1092b commit 3c480ca
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
17 changes: 1 addition & 16 deletions src/appender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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",
Expand All @@ -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,
}
}
Expand All @@ -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
}
}
20 changes: 7 additions & 13 deletions src/plugin/file_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}
Expand All @@ -110,13 +104,13 @@ impl RollingType {
}
}

fn file_name_parse_time(&self, name: &str, temp_name: &str) -> Option<NaiveDateTime> {
fn file_name_parse_time(&self, name: &str, temp_name: &str) -> Option<fastdate::DateTime> {
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);
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3c480ca

Please sign in to comment.