create info and warn files separate from trace

This commit is contained in:
OMGeeky
2024-04-28 12:31:34 +02:00
parent a3499ea8bf
commit 153196685c
4 changed files with 41 additions and 19 deletions

15
Cargo.lock generated
View File

@@ -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",

View File

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

View File

@@ -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<WorkerGuard> {
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<NonBlocking>) {
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)
}

View File

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