From 153196685ced86bf063b0a98cbb726f15b269dd3 Mon Sep 17 00:00:00 2001 From: OMGeeky Date: Sun, 28 Apr 2024 12:31:34 +0200 Subject: [PATCH] create info and warn files separate from trace --- Cargo.lock | 15 ++++++++++++++- Cargo.toml | 4 ++-- src/lib.rs | 31 ++++++++++++++++++++----------- src/main.rs | 10 +++++----- 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 329f3c0..7711baa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2530,6 +2530,16 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-serde" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc6b213177105856957181934e4920de57730fc69bf42c37ee5bb664d406d9e1" +dependencies = [ + "serde", + "tracing-core", +] + [[package]] name = "tracing-subscriber" version = "0.3.18" @@ -2540,12 +2550,15 @@ dependencies = [ "nu-ansi-term", "once_cell", "regex", + "serde", + "serde_json", "sharded-slab", "smallvec", "thread_local", "tracing", "tracing-core", "tracing-log", + "tracing-serde", ] [[package]] @@ -2561,7 +2574,7 @@ dependencies = [ [[package]] name = "twba-common" -version = "0.1.0" +version = "0.2.0" dependencies = [ "tracing", "tracing-appender", diff --git a/Cargo.toml b/Cargo.toml index dd6987c..94d604d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,13 +1,13 @@ [package] name = "twba-common" -version = "0.1.0" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] tracing = "0.1" -tracing-subscriber = "0.3" +tracing-subscriber = { version = "0.3", features = ["json"] } tracing-appender = "0.2" diff --git a/src/lib.rs b/src/lib.rs index ccf007f..89e530d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,9 @@ use tracing::{info, Level}; use tracing_appender::non_blocking::{NonBlocking, WorkerGuard}; -use tracing_subscriber::fmt::format::{DefaultFields, Format}; +use tracing_appender::rolling::Rotation; +use tracing_subscriber::fmt::writer::{MakeWriterExt, WithMaxLevel}; use tracing_subscriber::fmt::{Layer, Subscriber}; use tracing_subscriber::layer::SubscriberExt; -use tracing_subscriber::{fmt, EnvFilter}; use twba_backup_config::Conf; pub fn get_config() -> Conf { @@ -12,30 +12,39 @@ pub fn get_config() -> Conf { .expect("Failed to load config") } -pub fn init_tracing(crate_name: &str) -> WorkerGuard { - let (guard, file) = file_tracer(crate_name); +pub fn init_tracing(crate_name: &str) -> Vec { + let (guard1, warn_file) = file_tracer(crate_name, Level::WARN, Rotation::HOURLY); + let (guard2, info_file) = file_tracer(crate_name, Level::INFO, Rotation::HOURLY); + let (guard3, trace_file) = file_tracer(crate_name, Level::TRACE, Rotation::HOURLY); let file_subscriber = Subscriber::builder() - .with_max_level(Level::INFO) .with_env_filter(format!("warn,{}=trace", crate_name)) .finish() - .with(Layer::default().with_writer(file)); + .with(Layer::default().with_writer(warn_file).json()) + .with(Layer::default().with_writer(info_file).json()) + .with(Layer::default().with_writer(trace_file).json()); // Set the layered subscriber as the global default tracing::subscriber::set_global_default(file_subscriber) .expect("Failed to set global default subscriber"); info!("Tracing initialized for {}", crate_name); - guard + vec![guard1, guard2, guard3] } -pub fn file_tracer(crate_name: &str) -> (WorkerGuard, NonBlocking) { +pub fn file_tracer( + crate_name: &str, + level: Level, + rotation: Rotation, +) -> (WorkerGuard, WithMaxLevel) { let dir = get_config().log_path(); let trace_writer = tracing_appender::rolling::RollingFileAppender::builder() - .rotation(tracing_appender::rolling::Rotation::HOURLY) - .filename_prefix(format!("{}-trace", crate_name)) - .filename_suffix("log") + .rotation(rotation) + .filename_prefix(crate_name) + .filename_suffix(format!("{}.{}", level, "log")) .build(dir) .unwrap(); + // let trace_writer = trace_writer.with_max_level(Level::TRACE); let (file, guard) = tracing_appender::non_blocking(trace_writer); + let file = file.with_max_level(level); (guard, file) } diff --git a/src/main.rs b/src/main.rs index c758b39..b530762 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,11 @@ use tracing::*; use twba_common::init_tracing; pub fn main() { - let _guard = init_tracing("common"); + let _guard = init_tracing("twba_common"); - info!("Hello, world! info"); - error!("Hello, world! error"); - warn!("Hello, world! warn"); - debug!("Hello, world! debug"); trace!("Hello, world! trace"); + debug!("Hello, world! debug"); + info!("Hello, world! info"); + warn!("Hello, world! warn"); + error!("Hello, world! error"); }