tracing
This commit is contained in:
parent
7df635ef18
commit
b1fd4f2b9e
@ -8,4 +8,7 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.79"
|
anyhow = "1.0.79"
|
||||||
chrono = "0.4.33"
|
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"] }
|
||||||
|
|||||||
@ -4,7 +4,7 @@ use std::thread;
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
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 {
|
for _ in 0..100 {
|
||||||
let _ = roller.write("hello".as_bytes())?;
|
let _ = roller.write("hello".as_bytes())?;
|
||||||
thread::sleep(Duration::from_millis(100));
|
thread::sleep(Duration::from_millis(100));
|
||||||
|
|||||||
10
examples/tracing-log/main.rs
Normal file
10
examples/tracing-log/main.rs
Normal 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();
|
||||||
|
}
|
||||||
10
examples/tracing-log/submod.rs
Normal file
10
examples/tracing-log/submod.rs
Normal 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");
|
||||||
|
}
|
||||||
27
src/lib.rs
27
src/lib.rs
@ -1,3 +1,6 @@
|
|||||||
|
use time::macros::{format_description, offset};
|
||||||
|
use tracing_subscriber::fmt::time::OffsetTime;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::fs;
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user