Skip to content

Commit

Permalink
add CanRollingPack
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Jul 27, 2024
1 parent 5336a52 commit df90d03
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 71 deletions.
35 changes: 17 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,18 @@ fn main(){

```rust
use fast_log::config::Config;
use fast_log::plugin::file_split::{PackType, KeepType, DateType, HowPack};
use fast_log::plugin::file_split::{RollingType, KeepType, DateType, Rolling};
use std::thread::sleep;
use std::time::Duration;
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/",
HowPack::new(PackType::ByDate(DateType::Day)),
Rolling::new(RollingType::ByDate(DateType::Day)),
KeepType::KeepNum(2),
LogPacker {},
)).unwrap();
))
.unwrap();
for _ in 0..60 {
sleep(Duration::from_secs(1));
log::info!("Commencing yak shaving");
Expand All @@ -163,23 +162,23 @@ fn main() {
```rust
use fast_log::config::Config;
use fast_log::consts::LogSize;
use fast_log::plugin::file_split::{RollingType, KeepType, Rolling};
use fast_log::plugin::packer::LogPacker;
use fast_log::plugin::file_split::{PackType, KeepType, HowPack};

#[test]
pub fn test_file_compation() {
fast_log::init(Config::new()
.console()
.chan_len(Some(100000))
.file_split("target/logs/",
HowPack::new(PackType::BySize(LogSize::MB(1))),
KeepType::All,
LogPacker{})).unwrap();
for _ in 0..200000 {
info!("Commencing yak shaving");
fn main() {
fast_log::init(Config::new().chan_len(Some(100000)).console().file_split(
"target/logs/",
Rolling::new(RollingType::BySize(LogSize::KB(500))),
KeepType::KeepNum(2),
LogPacker {},
))
.unwrap();
for _ in 0..40000 {
log::info!("Commencing yak shaving");
}
log::logger().flush();
println!("you can see log files in path: {}", "target/logs/")
}

```


Expand Down
5 changes: 2 additions & 3 deletions example/src/split_log.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use fast_log::config::Config;
use fast_log::consts::LogSize;
use fast_log::plugin::file_split::{PackType, KeepType, HowPack};
use fast_log::plugin::file_split::{RollingType, KeepType, Rolling};
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/",
HowPack::new(PackType::BySize(LogSize::KB(500))),
Rolling::new(RollingType::BySize(LogSize::KB(500))),
KeepType::KeepNum(2),
LogPacker {},
))
Expand Down
6 changes: 2 additions & 4 deletions example/src/split_log_date.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use fast_log::config::Config;
use fast_log::plugin::file_split::{PackType, KeepType, DateType, HowPack};
use fast_log::plugin::file_split::{RollingType, KeepType, DateType, Rolling};
use std::thread::sleep;
use std::time::Duration;
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/",
HowPack::new(PackType::ByDate(DateType::Day)),
Rolling::new(RollingType::ByDate(DateType::Day)),
KeepType::KeepNum(2),
LogPacker {},
))
Expand Down
5 changes: 2 additions & 3 deletions example/src/split_log_duration.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use fast_log::config::Config;
use fast_log::plugin::file_split::{PackType, KeepType, HowPack};
use fast_log::plugin::file_split::{RollingType, KeepType, Rolling};
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/",
HowPack::new(PackType::ByDuration((DateTime::now(), Duration::from_secs(5)))),
Rolling::new(RollingType::ByDuration((DateTime::now(), Duration::from_secs(5)))),
KeepType::KeepNum(5),
LogPacker {},
))
Expand Down
4 changes: 2 additions & 2 deletions example/src/split_log_gz.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use fast_log::config::Config;
use fast_log::consts::LogSize;
use fast_log::plugin::file_split::{PackType, KeepType, HowPack};
use fast_log::plugin::file_split::{RollingType, KeepType, Rolling};
use fast_log::plugin::packer::GZipPacker;

fn main() {
fast_log::init(Config::new().chan_len(Some(100000)).console().file_split(
"target/logs/",
HowPack::new(PackType::BySize(LogSize::KB(50))),
Rolling::new(RollingType::BySize(LogSize::KB(50))),
KeepType::KeepNum(5),
GZipPacker {},
))
Expand Down
1 change: 0 additions & 1 deletion example/src/split_log_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use fast_log::consts::LogSize;

///Single logs are stored in rolling mode by capacity
fn main() {
//or: let file_name = "sloop.log"
let file_name = "target/logs/sloop.log";
fast_log::init(
Config::new()
Expand Down
4 changes: 2 additions & 2 deletions example/src/split_log_lz4.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use fast_log::config::Config;
use fast_log::consts::LogSize;
use fast_log::plugin::file_split::{PackType, KeepType, HowPack};
use fast_log::plugin::file_split::{RollingType, KeepType, Rolling};
use fast_log::plugin::packer::LZ4Packer;

fn main() {
fast_log::init(Config::new().chan_len(Some(100000)).console().file_split(
"target/logs/",
HowPack::new(PackType::BySize(LogSize::KB(50))),
Rolling::new(RollingType::BySize(LogSize::KB(50))),
KeepType::KeepNum(5),
LZ4Packer {},
))
Expand Down
4 changes: 2 additions & 2 deletions example/src/split_log_zip.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use fast_log::consts::LogSize;
use fast_log::plugin::file_split::{PackType, KeepType, HowPack};
use fast_log::plugin::file_split::{RollingType, KeepType, Rolling};
use fast_log::plugin::packer::ZipPacker;

use fast_log::config::Config;

fn main() {
fast_log::init(Config::new().chan_len(Some(100000)).console().file_split(
"target/logs/",
HowPack::new(PackType::BySize(LogSize::KB(50))),
Rolling::new(RollingType::BySize(LogSize::KB(50))),
KeepType::KeepNum(5),
ZipPacker {},
))
Expand Down
24 changes: 12 additions & 12 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::filter::Filter;
use crate::plugin::console::ConsoleAppender;
use crate::plugin::file::FileAppender;
use crate::plugin::file_loop::FileLoopAppender;
use crate::plugin::file_split::{FileSplitAppender, CanPack, Keep, Packer, RawFile, SplitFile};
use crate::plugin::file_split::{FileSplitAppender, CanRollingPack, Keep, Packer, RawFile, SplitFile};
use crate::FastLogFormat;
use dark_std::sync::SyncVec;
use log::LevelFilter;
Expand Down Expand Up @@ -102,21 +102,21 @@ impl Config {
self
}
/// add a FileSplitAppender
pub fn file_split<P: Packer + Sync + 'static, R: Keep + 'static,H: CanPack +'static>(
pub fn file_split<Packer: Packer + Sync + 'static, Keeper: Keep + 'static, Rolling: CanRollingPack + 'static>(
self,
file_path: &str,
how:H,
rolling_type: R,
packer: P,
rolling: Rolling,
keeper: Keeper,
packer: Packer,
) -> Self {
self.appends.push(Mutex::new(Box::new(
FileSplitAppender::new::<RawFile>(
file_path,
Box::new(how),
Box::new(rolling_type),
Box::new(rolling),
Box::new(keeper),
Box::new(packer),
)
.unwrap(),
.unwrap(),
)));
self
}
Expand All @@ -127,7 +127,7 @@ impl Config {
/// ```rust
/// use fast_log::Config;
/// use fast_log::consts::LogSize;
/// use fast_log::plugin::file_split::{HowPack, PackType, RawFile, RollingType};
/// use fast_log::plugin::file_split::{Rolling, RollingType, RawFile, RollingType};
/// use fast_log::plugin::packer::LogPacker;
/// fn new(){
/// fast_log::init(
Expand All @@ -137,17 +137,17 @@ impl Config {
/// "target/logs/temp.log",
/// RollingType::All,
/// LogPacker {},
/// HowPack::new(PackType::BySize(LogSize::MB(1))),
/// Rolling::new(RollingType::BySize(LogSize::MB(1))),
/// ),
/// );
/// }
/// ```
pub fn split<F: SplitFile + 'static, R: Keep + 'static, P: Packer + Sync + 'static,H: CanPack +'static>(
pub fn split<F: SplitFile + 'static, R: Keep + 'static, P: Packer + Sync + 'static, H: CanRollingPack + 'static>(
self,
file_path: &str,
keeper: R,
packer: P,
how_pack:H,
how_pack: H,
) -> Self {
self.appends.push(Mutex::new(Box::new(
FileSplitAppender::new::<F>(file_path, Box::new(how_pack), Box::new(keeper), Box::new(packer)).unwrap(),
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, HowPack};
use crate::plugin::file_split::{FileSplitAppender, RollingType, KeepType, RawFile, Rolling};
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(HowPack::new(PackType::BySize(size))),
Box::new(Rolling::new(RollingType::BySize(size))),
Box::new(KeepType::KeepNum(1)),
Box::new(LogPacker {}),
)?,
Expand Down
37 changes: 17 additions & 20 deletions src/plugin/file_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ impl Packer for Box<dyn Packer> {
}

/// is can do pack?
pub trait CanPack: Send {
fn is_pack(&mut self, appender: &dyn Packer, temp_name: &str, temp_size: usize, arg: &FastLogRecord) -> Option<String>;
pub trait CanRollingPack: Send {
fn can(&mut self, appender: &dyn Packer, temp_name: &str, temp_size: usize, arg: &FastLogRecord) -> Option<String>;
}

/// keep logs, for example keep by log num or keep by log create time.
Expand Down Expand Up @@ -190,32 +190,33 @@ impl DurationType {
}


pub struct HowPack {
pub struct Rolling {
last: SystemTime,
pub how: PackType,
pub how: RollingType,
}

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

pub enum PackType {
///log rolling type
pub enum RollingType {
ByDate(DateType),
BySize(LogSize),
ByDuration((DateTime, Duration)),
}

impl CanPack for HowPack {
fn is_pack(&mut self, _appender: &dyn Packer, temp_name: &str, temp_size: usize, arg: &FastLogRecord) -> Option<String> {
impl CanRollingPack for Rolling {
fn can(&mut self, _appender: &dyn Packer, temp_name: &str, temp_size: usize, arg: &FastLogRecord) -> Option<String> {
let last_time = self.last.clone();
self.last = arg.now.clone();
return match &mut self.how {
PackType::ByDate(date_type) => {
RollingType::ByDate(date_type) => {
let last_time = DateTime::from_system_time(last_time, fastdate::offset_sec());
let log_time = DateTime::from_system_time(arg.now, fastdate::offset_sec());
let diff = match date_type {
Expand Down Expand Up @@ -254,7 +255,7 @@ impl CanPack for HowPack {
None
}
}
PackType::BySize(limit) => {
RollingType::BySize(limit) => {
if temp_size >= limit.get_len() {
let log_name = {
let last_time = DateTime::from_system_time(last_time, fastdate::offset_sec());
Expand All @@ -272,7 +273,7 @@ impl CanPack for HowPack {
None
}
}
PackType::ByDuration((start_time, duration)) => {
RollingType::ByDuration((start_time, duration)) => {
let log_time = DateTime::from_system_time(arg.now, fastdate::offset_sec());
let next = start_time.clone().add(duration.clone());
if log_time >= next {
Expand Down Expand Up @@ -305,7 +306,7 @@ pub struct FileSplitAppender {
packer: Arc<Box<dyn Packer>>,
dir_path: String,
sender: Sender<LogPack>,
can_pack: Box<dyn CanPack>,
can_pack: Box<dyn CanRollingPack>,
//cache data
temp_bytes: AtomicUsize,
temp_name: String,
Expand All @@ -314,7 +315,7 @@ pub struct FileSplitAppender {
impl FileSplitAppender {
pub fn new<F: SplitFile + 'static>(
file_path: &str,
how_to_pack: Box<dyn CanPack>,
how_to_pack: Box<dyn CanRollingPack>,
rolling_type: Box<dyn Keep>,
packer: Box<dyn Packer>,
) -> Result<FileSplitAppender, LogError> {
Expand Down Expand Up @@ -434,10 +435,6 @@ impl LogPack {
}
}


///rolling keep type
pub type RollingType = KeepType;

///rolling keep type
#[derive(Copy, Clone, Debug)]
pub enum KeepType {
Expand Down Expand Up @@ -499,7 +496,7 @@ impl LogAppender for FileSplitAppender {
let current_temp_size = self.temp_bytes.load(Ordering::Relaxed)
+ temp.as_bytes().len()
+ x.formated.as_bytes().len();
if let Some(new_log_name) = self.can_pack.is_pack(self.packer.deref(), &self.temp_name, current_temp_size, x) {
if let Some(new_log_name) = self.can_pack.can(self.packer.deref(), &self.temp_name, current_temp_size, x) {
self.temp_bytes.fetch_add(
{
let w = self.file.write(temp.as_bytes());
Expand All @@ -519,7 +516,7 @@ impl LogAppender for FileSplitAppender {
Command::CommandExit => {}
Command::CommandFlush(ref w) => {
let current_temp_size = self.temp_bytes.load(Ordering::Relaxed);
if let Some(new_log_name) = self.can_pack.is_pack(self.packer.deref(), &self.temp_name, current_temp_size, x) {
if let Some(new_log_name) = self.can_pack.can(self.packer.deref(), &self.temp_name, current_temp_size, x) {
self.temp_bytes.fetch_add(
{
let w = self.file.write(temp.as_bytes());
Expand Down
4 changes: 2 additions & 2 deletions tests/split_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod test {
use fast_log::appender::{Command, FastLogRecord, LogAppender};
use fast_log::consts::LogSize;
use fast_log::plugin::file_name::FileName;
use fast_log::plugin::file_split::{FileSplitAppender, PackType, Keep, RawFile, RollingType, HowPack};
use fast_log::plugin::file_split::{FileSplitAppender, RollingType, Keep, RawFile, RollingType, Rolling};
use fast_log::plugin::packer::LogPacker;
use fastdate::DateTime;
use log::Level;
Expand All @@ -16,7 +16,7 @@ mod test {
let _ = remove_dir_all("target/test/");
let mut appender = FileSplitAppender::new::<RawFile>(
"target/test/",
Box::new(HowPack::new(PackType::BySize(LogSize::MB(1)))),
Box::new(Rolling::new(RollingType::BySize(LogSize::MB(1)))),
Box::new(RollingType::All),
Box::new(LogPacker {}),
)
Expand Down

0 comments on commit df90d03

Please sign in to comment.