Skip to content

Commit

Permalink
Multithreaded logging
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Nov 7, 2020
1 parent 57032fd commit aea96b8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 37 deletions.
20 changes: 18 additions & 2 deletions src/fast_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct FastLogRecord {
pub file: String,
pub line: Option<u32>,
pub now: DateTime<Local>,
pub formated: String,
}

impl FastLogRecord {
Expand All @@ -33,6 +34,18 @@ impl FastLogRecord {
(file, line) => format!("({}:{})", file, line),
}
}
pub fn set_formated(&mut self) {
let data;
match self.level {
Level::Warn | Level::Error => {
data = format!("{} {} {} - {} {}\n", &self.now, self.level, self.module_path, self.args, self.format_line());
}
_ => {
data = format!("{} {} {} - {}\n", &self.now, self.level, self.module_path, self.args);
}
}
self.formated = data;
}
}

pub struct LoggerSender {
Expand Down Expand Up @@ -110,15 +123,18 @@ impl log::Log for Logger {
match lock.deref() {
Some(sender) => {
if !sender.filter.filter(record) {
sender.send(FastLogRecord {
let mut fast_log_record = FastLogRecord {
level,
target: record.metadata().target().to_string(),
args: record.args().to_string(),
module_path: record.module_path().unwrap_or("").to_string(),
file: record.file().unwrap_or("").to_string(),
line: record.line().clone(),
now: Local::now(),
});
formated: "".to_string(),
};
fast_log_record.set_formated();
sender.send(fast_log_record);
}
}
_ => {}
Expand Down
13 changes: 1 addition & 12 deletions src/plugin/console.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
use crate::fast_log::{LogAppender, FastLogRecord};
use log::Level;


/// only write append into console
pub struct ConsoleAppender {}

impl LogAppender for ConsoleAppender {
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);
print!("{}", record.formated);
}
}
13 changes: 1 addition & 12 deletions src/plugin/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use std::cell::RefCell;
use std::fs::{File, OpenOptions};
use std::io::Write;

use log::Level;

use crate::fast_log::{FastLogRecord, LogAppender};


Expand All @@ -26,16 +24,7 @@ impl FileAppender {

impl LogAppender for FileAppender {
fn do_log(&self, record: &FastLogRecord){
let mut data = String::new();
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);
}
}
self.file.borrow_mut().write(data.as_bytes());
self.file.borrow_mut().write(record.formated.as_bytes());
self.file.borrow_mut().flush();
}
}
13 changes: 2 additions & 11 deletions src/plugin/file_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::fs::{DirBuilder, File, OpenOptions};
use std::io::{Read, Write};
use std::path::Path;
use chrono::{Local};
use log::Level;

use crate::fast_log::{FastLogRecord, LogAppender};
use zip::write::FileOptions;
Expand Down Expand Up @@ -62,15 +61,7 @@ impl FileSplitAppender {

impl LogAppender for FileSplitAppender {
fn do_log(&self, record: &FastLogRecord) {
let mut log = String::new();
match record.level {
Level::Warn | Level::Error => {
log = format!("{} {} {} - {} {}\n", &record.now, record.level, record.module_path, record.args, record.format_line());
}
_ => {
log = format!("{} {} {} - {}\n", &record.now, record.level, record.module_path, record.args);
}
}
let mut log_data = record.formated.as_str();
let mut data=self.cell.borrow_mut();
if data.temp_log_num >= data.split_log_num {
let current_file_path = format!("{}{}.log", data.dir_path.to_string(), data.create_num);
Expand All @@ -92,7 +83,7 @@ impl LogAppender for FileSplitAppender {
data.create_num -= 1;
}
}
data.file.write(log.as_bytes());
data.file.write(log_data.as_bytes());
data.file.flush();
data.temp_log_num += 1;
}
Expand Down

0 comments on commit aea96b8

Please sign in to comment.