This commit is contained in:
asxalex 2024-01-31 21:47:09 +08:00
parent 7df635ef18
commit b1fd4f2b9e
5 changed files with 52 additions and 2 deletions

View File

@ -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"] }

View File

@ -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));

View File

@ -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();
}

View File

@ -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");
}

View File

@ -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<P: AsRef<Path>>(
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::*;