Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed May 25, 2022
1 parent 6f12822 commit 0d2706f
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ members = [

[package]
name = "fast_log"
version = "1.5.14"
version = "1.5.15"
description = "Rust async log High-performance asynchronous logging"
readme = "Readme.md"
authors = ["ce <[email protected]>"]
Expand Down
3 changes: 3 additions & 0 deletions example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ path = "src/custom_appender_tokio.rs"
[[bin]]
name = "custom_appender_meilisearch"
path = "src/custom_appender_meilisearch.rs"
[[bin]]
name = "format_log"
path = "src/format_log.rs"
[dependencies]
log = { version = "0.4", features = ["std"] }
crossbeam-channel = "0.5"
Expand Down
2 changes: 1 addition & 1 deletion example/src/custom_appender.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::time::Duration;
use fast_log::appender::{FastLogFormatRecord, LogAppender, FastLogRecord};
use fast_log::appender::{FastLogFormat, LogAppender, FastLogRecord};
use fast_log::filter::NoFilter;
use log::Level;
use std::thread::sleep;
Expand Down
2 changes: 1 addition & 1 deletion example/src/custom_appender_meilisearch.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use fast_log::appender::{FastLogFormatRecord, LogAppender, FastLogRecord};
use fast_log::appender::{FastLogFormat, LogAppender, FastLogRecord};
use fast_log::filter::NoFilter;
use log::Level;
use std::thread::sleep;
Expand Down
2 changes: 1 addition & 1 deletion example/src/custom_appender_tokio.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::time::Duration;
use fast_log::appender::{FastLogFormatRecord, LogAppender, FastLogRecord};
use fast_log::appender::{FastLogFormat, LogAppender, FastLogRecord};
use fast_log::filter::NoFilter;
use log::Level;
use std::thread::sleep;
Expand Down
11 changes: 11 additions & 0 deletions example/src/format_log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use log::LevelFilter;
use fast_log::appender::FastLogFormat;
use fast_log::config::Config;

fn main() {
fast_log::init(Config::new()
.format(FastLogFormat::new().set_display_line_level(LevelFilter::Trace)
).console()).unwrap();
log::info!("Commencing yak shaving{}", 0);
log::logger().flush();
}
2 changes: 1 addition & 1 deletion example/src/split_log_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use fast_log::plugin::file_split::RollingType;
use fast_log::plugin::packer::{LogPacker};
use std::thread::sleep;
use std::time::Duration;
use fast_log::appender::FastLogFormatRecord;
use fast_log::appender::FastLogFormat;
use fast_log::config::Config;
use fast_log::filter::NoFilter;
use fast_log::plugin::file_loop::FileLoopAppender;
Expand Down
37 changes: 26 additions & 11 deletions src/appender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ impl PartialEq for Command {
self.to_i32().eq(&other.to_i32())
}
}
impl Eq for Command{}

impl Eq for Command {}

#[derive(Clone, Debug)]
pub struct FastLogRecord {
Expand All @@ -65,19 +66,21 @@ pub trait RecordFormat: Send + Sync {
fn do_format(&self, arg: &mut FastLogRecord) -> String;
}

pub struct FastLogFormatRecord {
pub duration: Duration,
pub display_file: log::LevelFilter,
pub struct FastLogFormat {
// Time zone Interval hour
pub duration_zone: Duration,
// show line level
pub display_line_level: log::LevelFilter,
}


impl RecordFormat for FastLogFormatRecord {
impl RecordFormat for FastLogFormat {
fn do_format(&self, arg: &mut FastLogRecord) -> String {
match arg.command {
CommandRecord => {
let data;
let now = date::LogDate::from(arg.now.add(self.duration));
if arg.level.to_level_filter() <= self.display_file {
let now = date::LogDate::from(arg.now.add(self.duration_zone));
if arg.level.to_level_filter() <= self.display_line_level {
data = format!(
"{:26} {} {} - {} {}:{}\n",
&now,
Expand All @@ -102,17 +105,29 @@ impl RecordFormat for FastLogFormatRecord {
}
}

impl FastLogFormatRecord {
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() -> FastLogFormatRecord {
pub fn new() -> FastLogFormat {
Self {
duration: Self::local_duration(),
display_file: LevelFilter::Warn,
duration_zone: Self::local_duration(),
display_line_level: LevelFilter::Warn,
}
}

///show line level
pub fn set_display_line_level(mut self, level: LevelFilter) -> Self {
self.display_line_level = level;
self
}

/// Time zone Interval hour
pub fn set_duration(mut self, duration: Duration) -> Self {
self.duration_zone = duration;
self
}
}
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use log::{LevelFilter};
use crate::appender::{FastLogFormatRecord, LogAppender, RecordFormat};
use crate::appender::{FastLogFormat, LogAppender, RecordFormat};
use crate::consts::LogSize;
use crate::filter::{Filter, NoFilter};
use crate::plugin::console::ConsoleAppender;
Expand All @@ -20,7 +20,7 @@ impl Default for Config {
appends: vec![],
level: LevelFilter::Info,
filter: Box::new(NoFilter {}),
format: Box::new(FastLogFormatRecord::new()),
format: Box::new(FastLogFormat::new()),
}
}
}
Expand Down

0 comments on commit 0d2706f

Please sign in to comment.