From b1fd4f2b9e5e4c01c271d2e39867a34ade14fc67 Mon Sep 17 00:00:00 2001 From: asxalex Date: Wed, 31 Jan 2024 21:47:09 +0800 Subject: [PATCH] tracing --- Cargo.toml | 5 ++++- examples/exp1/main.rs | 2 +- examples/tracing-log/main.rs | 10 ++++++++++ examples/tracing-log/submod.rs | 10 ++++++++++ src/lib.rs | 27 +++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 examples/tracing-log/main.rs create mode 100644 examples/tracing-log/submod.rs diff --git a/Cargo.toml b/Cargo.toml index cb1cdac..0382319 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,7 @@ edition = "2021" [dependencies] anyhow = "1.0.79" chrono = "0.4.33" -time = "0.3.31" +time = { version = "0.3.31", features = ["macros"] } +tracing = "0.1.40" +tracing-appender = "0.2.3" +tracing-subscriber = { version = "0.3.18", features = ["time"] } diff --git a/examples/exp1/main.rs b/examples/exp1/main.rs index a2f1b37..bb359ab 100644 --- a/examples/exp1/main.rs +++ b/examples/exp1/main.rs @@ -4,7 +4,7 @@ use std::thread; use std::time::Duration; fn main() -> std::io::Result<()> { - let mut roller = FileRoller::new("output", 8, PeriodGap::Secondly); + let mut roller = FileRoller::new(".output", 8, PeriodGap::Secondly); for _ in 0..100 { let _ = roller.write("hello".as_bytes())?; thread::sleep(Duration::from_millis(100)); diff --git a/examples/tracing-log/main.rs b/examples/tracing-log/main.rs new file mode 100644 index 0000000..54d87e4 --- /dev/null +++ b/examples/tracing-log/main.rs @@ -0,0 +1,10 @@ +use rolling_file::default; +use time::macros::{format_description, offset}; +use tracing_subscriber::fmt::time::OffsetTime; + +mod submod; + +fn main() { + let _guard = default(); + submod::do_record(); +} diff --git a/examples/tracing-log/submod.rs b/examples/tracing-log/submod.rs new file mode 100644 index 0000000..35cd680 --- /dev/null +++ b/examples/tracing-log/submod.rs @@ -0,0 +1,10 @@ +use tracing::Level; +use tracing::{debug, error, info, trace, warn}; + +pub fn do_record() { + trace!("trace"); + debug!("debug"); + info!("information"); + warn!("warning"); + error!("error"); +} diff --git a/src/lib.rs b/src/lib.rs index 0116b2e..ceceb3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,6 @@ +use time::macros::{format_description, offset}; +use tracing_subscriber::fmt::time::OffsetTime; + use std::env; use std::ffi::OsString; use std::fs; @@ -208,6 +211,30 @@ fn test_offset() { } */ +pub fn new_log>( + path: P, + maxfile: usize, + period: PeriodGap, + level: tracing::Level, +) -> tracing_appender::non_blocking::WorkerGuard { + let timefmt = format_description!("[year]-[month]-[day]T[hour]:[minute]:[second]"); + let timer = OffsetTime::new(offset!(+8), timefmt); + let roller = FileRoller::new(path, maxfile, period); + let (writer, guard) = tracing_appender::non_blocking(roller); + tracing_subscriber::fmt() + .with_writer(writer) + .with_max_level(level) + .with_timer(timer) + // .with_timer(LocalTime::rfc_3339()) + .init(); + + guard +} + +pub fn default() -> tracing_appender::non_blocking::WorkerGuard { + new_log("./.output", 7, PeriodGap::Daily, tracing::Level::DEBUG) +} + #[cfg(test)] mod tests { use super::*;