From d1e24e7eec0ecef163ab1c3b99286aafafa0bdc9 Mon Sep 17 00:00:00 2001 From: Vika Date: Sat, 22 Jul 2023 12:35:39 +0300 Subject: Log with tracing-tree AND json in debug builds --- .gitignore | 3 ++- kittybox-rs/src/main.rs | 42 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index ceff1aa..6df432e 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,8 @@ dump.rdb .\#* .*~ *~ -/.log +*.log +*.log.json /kittybox-rs/test-dir /kittybox-rs/media-store /kittybox-rs/auth-store diff --git a/kittybox-rs/src/main.rs b/kittybox-rs/src/main.rs index 8c32556..6389489 100644 --- a/kittybox-rs/src/main.rs +++ b/kittybox-rs/src/main.rs @@ -258,17 +258,42 @@ where async fn main() { use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry}; - Registry::default() + let tracing_registry = Registry::default() .with(EnvFilter::from_default_env()) - //.with(tracing_subscriber::fmt::layer().json()) .with( #[cfg(debug_assertions)] - tracing_tree::HierarchicalLayer::new(2), + tracing_tree::HierarchicalLayer::new(2) + .with_bracketed_fields(true) + .with_indent_lines(true) + .with_verbose_exit(true), #[cfg(not(debug_assertions))] tracing_subscriber::fmt::layer().json() - ) - .init(); - //let _ = tracing_log::LogTracer::init(); + .with_ansi(std::io::IsTerminal::is_terminal(&std::io::stdout().lock())) + ); + // In debug builds, also log to JSON, but to file. + #[cfg(debug_assertions)] + let tracing_registry = tracing_registry.with( + tracing_subscriber::fmt::layer() + .json() + .with_writer({ + let instant = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap(); + move || std::fs::OpenOptions::new() + .append(true) + .create(true) + .open( + format!( + "{}.log.json", + instant + .as_secs_f64() + .to_string() + .replace('.', "_") + ) + ).unwrap() + }) + ); + tracing_registry.init(); tracing::info!("Starting the kittybox server..."); @@ -433,6 +458,11 @@ async fn main() { 1 } + _ = cancellation_token.cancelled() => { + tracing::info!("Signal caught from watchdog."); + + 0 + } _ = shutdown_signal => { tracing::info!("Shutdown requested by signal."); cancellation_token.cancel(); -- cgit 1.4.1