From fae38045d140050468f3c111da85630350684b95 Mon Sep 17 00:00:00 2001 From: zhuxiujia Date: Sun, 3 Jan 2021 19:09:22 +0800 Subject: [PATCH] add RecordFormat --- src/appender.rs | 22 ++++-- src/example/fast_log_test.rs | 116 ++++++++++++++++++++++++++++++ src/example/mod.rs | 1 + src/fast_log.rs | 136 +++-------------------------------- src/lib.rs | 6 +- 5 files changed, 148 insertions(+), 133 deletions(-) create mode 100644 src/example/fast_log_test.rs create mode 100644 src/example/mod.rs diff --git a/src/appender.rs b/src/appender.rs index 1297cae..c6ca6c1 100644 --- a/src/appender.rs +++ b/src/appender.rs @@ -30,17 +30,27 @@ impl FastLogRecord { (file, line) => format!("({}:{})", file, line), } } - pub fn set_formated(&mut self) { +} + +/// format record data +pub trait RecordFormat: Send { + fn do_format(&self, arg: &mut FastLogRecord); +} + +pub struct FastLogFormatRecord {} + +impl RecordFormat for FastLogFormatRecord { + fn do_format(&self, arg: &mut FastLogRecord) { let data; - let now = format!("{:36}",self.now.to_string()); - match self.level { + let now = format!("{:36}", arg.now.to_string()); + match arg.level { Level::Warn | Level::Error => { - data = format!("{} {} {} - {} {}\n", &now, self.level, self.module_path, self.args, self.format_line()); + data = format!("{} {} {} - {} {}\n", &now, arg.level, arg.module_path, arg.args, arg.format_line()); } _ => { - data = format!("{} {} {} - {}\n", &now, self.level, self.module_path, self.args); + data = format!("{} {} {} - {}\n", &now, arg.level, arg.module_path, arg.args); } } - self.formated = data; + arg.formated = data; } } \ No newline at end of file diff --git a/src/example/fast_log_test.rs b/src/example/fast_log_test.rs new file mode 100644 index 0000000..69cee68 --- /dev/null +++ b/src/example/fast_log_test.rs @@ -0,0 +1,116 @@ +#[cfg(test)] +mod test { + use std::thread::sleep; + use std::time::{Duration, Instant}; + + use log::{debug, info, Level}; + use log::error; + + use crate::{init_custom_log, init_log, init_split_log}; + use crate::bencher::QPS; + use crate::consts::LogSize; + use crate::filter::NoFilter; + use crate::plugin::file_split::RollingType; + use crate::appender::{FastLogFormatRecord, LogAppender, FastLogRecord}; + + #[test] + pub fn test_log() { + init_log("requests.log", 1000, log::Level::Debug, None, true); + debug!("Commencing yak shaving{}", 0); + sleep(Duration::from_secs(1)); + } + + //cargo test --release --color=always --package fast_log --lib fast_log::test::bench_log --no-fail-fast -- --exact -Z unstable-options --show-output + #[test] + pub fn bench_log() { + init_log("requests.log", 1000, log::Level::Info, None, false); + let total = 10000; + let now = Instant::now(); + for index in 0..total { + //sleep(Duration::from_secs(1)); + info!("Commencing yak shaving{}", index); + } + now.time(total); + now.qps(total); + sleep(Duration::from_secs(1)); + } + + struct CustomLog {} + + impl LogAppender for CustomLog { + fn do_log(&self, record: &FastLogRecord) { + let data; + match record.level { + Level::Warn | Level::Error => { + data = format!("{} {} {} - {} {}\n", &record.now, record.level, record.module_path, record.args, record.format_line()); + } + _ => { + data = format!("{} {} {} - {}\n", &record.now, record.level, record.module_path, record.args); + } + } + print!("{}", data); + } + } + + #[test] + pub fn test_custom() { + init_custom_log(vec![Box::new(CustomLog {})], 1000, log::Level::Info, Box::new(NoFilter {}),Box::new(FastLogFormatRecord{})); + info!("Commencing yak shaving"); + error!("Commencing error"); + sleep(Duration::from_secs(1)); + } + + #[test] + pub fn test_file_compation() { + init_split_log("target/logs/", 1000, LogSize::MB(1), false, RollingType::All, log::Level::Info, None, true); + for _ in 0..20000 { + info!("Commencing yak shaving"); + } + sleep(Duration::from_secs(1)); + } + + #[test] + pub fn test_file_compation_zip() { + init_split_log("target/logs/", 1000, LogSize::KB(50), true, RollingType::KeepNum(5), log::Level::Info, None, true); + for _ in 0..20000 { + info!("Commencing yak shaving"); + } + sleep(Duration::from_secs(10)); + } + + #[test] + pub fn test_file_compation_zip_stable_test() { + init_split_log("target/logs/", 1000, LogSize::MB(100), true, RollingType::All, log::Level::Info, None, false); + let now = std::time::Instant::now(); + loop { + info!("Commencing yak shaving"); + if now.elapsed() > Duration::from_secs(30) { + break; + } + } + info!("done"); + sleep(Duration::from_secs(100)); + } + + + struct BenchRecvLog {} + + impl LogAppender for BenchRecvLog { + fn do_log(&self, record: &FastLogRecord) {} + } + + //cargo test --release --color=always --package fast_log --lib fast_log::test::bench_recv --no-fail-fast -- --exact -Z unstable-options --show-output + #[test] + pub fn bench_recv() { + init_custom_log(vec![Box::new(BenchRecvLog {})], 1000, log::Level::Info, Box::new(NoFilter {}),Box::new(FastLogFormatRecord{})); + let total = 10000; + let now = Instant::now(); + for index in 0..total { + //sleep(Duration::from_secs(1)); + info!("Commencing yak shaving{}", index); + } + now.time(total); + now.qps(total); + sleep(Duration::from_secs(1)); + } +} \ No newline at end of file diff --git a/src/example/mod.rs b/src/example/mod.rs new file mode 100644 index 0000000..da29102 --- /dev/null +++ b/src/example/mod.rs @@ -0,0 +1 @@ +mod fast_log_test; \ No newline at end of file diff --git a/src/fast_log.rs b/src/fast_log.rs index 5a66864..995b578 100644 --- a/src/fast_log.rs +++ b/src/fast_log.rs @@ -6,7 +6,7 @@ use chrono::Local; use crossbeam_channel::{Receiver, SendError}; use log::{Level, Metadata, Record}; -use crate::appender::{FastLogRecord, LogAppender}; +use crate::appender::{FastLogRecord, LogAppender, RecordFormat, FastLogFormatRecord}; use crate::consts::LogSize; use crate::error::LogError; use crate::filter::{Filter, NoFilter}; @@ -93,7 +93,7 @@ impl log::Log for Logger { match lock.deref() { Some(sender) => { if !sender.filter.filter(record) { - let mut fast_log_record = FastLogRecord { + let fast_log_record = FastLogRecord { level, target: record.metadata().target().to_string(), args: record.args().to_string(), @@ -103,7 +103,6 @@ impl log::Log for Logger { now: Local::now(), formated: "".to_string(), }; - fast_log_record.set_formated(); sender.send(fast_log_record); } } @@ -134,7 +133,7 @@ pub fn init_log(log_file_path: &str, channel_cup: usize, level: log::Level, mut if filter.is_some() { log_filter = filter.take().unwrap(); } - return init_custom_log(appenders, channel_cup, level, log_filter); + return init_custom_log(appenders, channel_cup, level, log_filter,Box::new(FastLogFormatRecord{})); } /// initializes the log file path @@ -145,7 +144,7 @@ pub fn init_log(log_file_path: &str, channel_cup: usize, level: log::Level, mut /// filter: log filter pub fn init_split_log(log_dir_path: &str, channel_log_cup: usize, max_temp_size: LogSize, allow_zip_compress: bool, rolling_type: RollingType, level: log::Level, mut filter: Option>, debug_mode: bool) -> Result<(), Box> { let mut appenders: Vec> = vec![ - Box::new(FileSplitAppender::new(log_dir_path, max_temp_size, rolling_type,allow_zip_compress,1)) + Box::new(FileSplitAppender::new(log_dir_path, max_temp_size, rolling_type, allow_zip_compress, 1)) ]; if debug_mode { appenders.push(Box::new(ConsoleAppender {})); @@ -154,10 +153,10 @@ pub fn init_split_log(log_dir_path: &str, channel_log_cup: usize, max_temp_size: if filter.is_some() { log_filter = filter.take().unwrap(); } - return init_custom_log(appenders, channel_log_cup, level, log_filter); + return init_custom_log(appenders, channel_log_cup, level, log_filter,Box::new(FastLogFormatRecord{})); } -pub fn init_custom_log(appenders: Vec>, log_cup: usize, level: log::Level, filter: Box) -> Result<(), Box> { +pub fn init_custom_log(appenders: Vec>, log_cup: usize, level: log::Level, filter: Box, format: Box) -> Result<(), Box> { if appenders.is_empty() { return Err(Box::new(LogError::from("[fast_log] appenders can not be empty!"))); } @@ -168,7 +167,8 @@ pub fn init_custom_log(appenders: Vec>, log_cup: usize, lev loop { let data = main_recv.recv(); if data.is_ok() { - let s: FastLogRecord = data.unwrap(); + let mut s: FastLogRecord = data.unwrap(); + format.do_format(&mut s); for x in &appenders { x.do_log(&s); } @@ -188,7 +188,8 @@ pub fn init_custom_log(appenders: Vec>, log_cup: usize, lev loop { let data = main_recv.recv(); if data.is_ok() { - let s: FastLogRecord = data.unwrap(); + let mut s: FastLogRecord = data.unwrap(); + format.do_format(&mut s); for x in &sends { x.send(s.clone()); } @@ -220,121 +221,4 @@ pub fn init_custom_log(appenders: Vec>, log_cup: usize, lev } else { return Ok(()); } -} - -#[cfg(test)] -mod test { - use std::thread::sleep; - use std::time::{Duration, Instant}; - - use log::{debug, info, Level}; - use log::error; - - use crate::{init_custom_log, init_log, init_split_log}; - use crate::bencher::QPS; - use crate::consts::LogSize; - use crate::fast_log::{FastLogRecord, LogAppender}; - use crate::filter::NoFilter; - use crate::plugin::file_split::RollingType; - - #[test] - pub fn test_log() { - init_log("requests.log", 1000, log::Level::Debug, None, true); - debug!("Commencing yak shaving{}", 0); - sleep(Duration::from_secs(1)); - } - - //cargo test --release --color=always --package fast_log --lib fast_log::test::bench_log --no-fail-fast -- --exact -Z unstable-options --show-output - #[test] - pub fn bench_log() { - init_log("requests.log", 1000, log::Level::Info, None, false); - let total = 10000; - let now = Instant::now(); - for index in 0..total { - //sleep(Duration::from_secs(1)); - info!("Commencing yak shaving{}", index); - } - now.time(total); - now.qps(total); - sleep(Duration::from_secs(1)); - } - - struct CustomLog {} - - impl LogAppender for CustomLog { - fn do_log(&self, record: &FastLogRecord) { - let data; - match record.level { - Level::Warn | Level::Error => { - data = format!("{} {} {} - {} {}\n", &record.now, record.level, record.module_path, record.args, record.format_line()); - } - _ => { - data = format!("{} {} {} - {}\n", &record.now, record.level, record.module_path, record.args); - } - } - print!("{}", data); - } - } - - #[test] - pub fn test_custom() { - init_custom_log(vec![Box::new(CustomLog {})], 1000, log::Level::Info, Box::new(NoFilter {})); - info!("Commencing yak shaving"); - error!("Commencing error"); - sleep(Duration::from_secs(1)); - } - - #[test] - pub fn test_file_compation() { - init_split_log("target/logs/", 1000, LogSize::MB(1), false, RollingType::All, log::Level::Info, None, true); - for _ in 0..20000 { - info!("Commencing yak shaving"); - } - sleep(Duration::from_secs(1)); - } - - #[test] - pub fn test_file_compation_zip() { - init_split_log("target/logs/", 1000, LogSize::KB(50), true, RollingType::KeepNum(5), log::Level::Info, None, true); - for _ in 0..20000 { - info!("Commencing yak shaving"); - } - sleep(Duration::from_secs(10)); - } - - #[test] - pub fn test_file_compation_zip_stable_test() { - init_split_log("target/logs/", 1000, LogSize::MB(100), true, RollingType::All, log::Level::Info, None, false); - let now = std::time::Instant::now(); - loop { - info!("Commencing yak shaving"); - if now.elapsed() > Duration::from_secs(30) { - break; - } - } - info!("done"); - sleep(Duration::from_secs(100)); - } - - - struct BenchRecvLog {} - - impl LogAppender for BenchRecvLog { - fn do_log(&self, record: &FastLogRecord) {} - } - - //cargo test --release --color=always --package fast_log --lib fast_log::test::bench_recv --no-fail-fast -- --exact -Z unstable-options --show-output - #[test] - pub fn bench_recv() { - init_custom_log(vec![Box::new(BenchRecvLog {})], 1000, log::Level::Info, Box::new(NoFilter {})); - let total = 10000; - let now = Instant::now(); - for index in 0..total { - //sleep(Duration::from_secs(1)); - info!("Commencing yak shaving{}", index); - } - now.time(total); - now.qps(total); - sleep(Duration::from_secs(1)); - } } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 2059ebe..fc19e48 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,4 +17,8 @@ pub mod consts; ///init log pub use fast_log::init_log as init_log; pub use fast_log::init_split_log as init_split_log; -pub use fast_log::init_custom_log as init_custom_log; \ No newline at end of file +pub use fast_log::init_custom_log as init_custom_log; + + +///test +mod example; \ No newline at end of file