about summary refs log tree commit diff
path: root/kittybox-rs
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2023-07-22 12:35:39 +0300
committerVika <vika@fireburn.ru>2023-07-22 12:35:39 +0300
commitd1e24e7eec0ecef163ab1c3b99286aafafa0bdc9 (patch)
tree7503c9d8271b14fc77fe25b3a1b3eee9dbc780ed /kittybox-rs
parent56ee62e8e034205e929caf608592261685107f4e (diff)
downloadkittybox-d1e24e7eec0ecef163ab1c3b99286aafafa0bdc9.tar.zst
Log with tracing-tree AND json in debug builds
Diffstat (limited to 'kittybox-rs')
-rw-r--r--kittybox-rs/src/main.rs42
1 files changed, 36 insertions, 6 deletions
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();