From fdd7d305b9e9cce1fe9f40a4fe3c5cd0d908e92b Mon Sep 17 00:00:00 2001 From: Einliterflasche Date: Mon, 12 Aug 2024 16:21:51 +0200 Subject: [PATCH] try to use tracing but fucking fail --- swap/src/asb/tracing.rs | 64 +++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/swap/src/asb/tracing.rs b/swap/src/asb/tracing.rs index 3393cee3..d7635d44 100644 --- a/swap/src/asb/tracing.rs +++ b/swap/src/asb/tracing.rs @@ -3,8 +3,10 @@ use std::path::Path; use anyhow::Result; use tracing_subscriber::filter::LevelFilter; use tracing_subscriber::fmt::time::UtcTime; -use tracing_subscriber::fmt::writer::MakeWriterExt; -use tracing_subscriber::FmtSubscriber; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::fmt; + + pub fn init( level: LevelFilter, @@ -18,21 +20,59 @@ pub fn init( let is_terminal = atty::is(atty::Stream::Stderr); - let appender = tracing_appender::rolling::never(dir.as_ref(), "swap-all.log"); + // File logger will always write in JSON format and with timestamps + let file_appender = tracing_appender::rolling::never(dir.as_ref(), "swap-all.log"); - let builder = FmtSubscriber::builder() - .with_env_filter(format!("asb={},swap={}", level, level)) - .with_writer(appender.and(std::io::stderr)) + let file_layer = fmt::layer() + .with_writer(std::io::stdout) + .with_ansi(false) + .with_timer(UtcTime::rfc_3339()) + .with_target(false) + .json(); + + // Terminal logger + let terminal_layer_base = fmt::layer() + .with_writer(std::io::stdout) .with_ansi(is_terminal) .with_timer(UtcTime::rfc_3339()) .with_target(false); - match (json_format, timestamp) { - (true, true) => builder.json().init(), - (true, false) => builder.json().without_time().init(), - (false, true) => builder.init(), - (false, false) => builder.without_time().init(), - } + // Since tracing is stupid, and makes each option return a different type + // but also doesn't allow dynamic dispatch we have to use this beauty + let ( + a, + b, + c, + d + ) = match (json_format, timestamp) { + (true, true) => (Some(terminal_layer_base.json()), None, None, None), + (true, false) => (None, Some(terminal_layer_base.json().without_time()), None, None), + (false, true) => (None, None, Some(terminal_layer_base), None), + (false, false) => (None, None, None, Some(terminal_layer_base.without_time())), + }; + + let combined_subscriber = tracing_subscriber::registry() + .with(file_layer) + .with(a); + + combined_subscriber.init(); + + // let builder = FmtSubscriber::builder() + // .with_env_filter(format!("asb={},swap={}", level, level)) + // .with_writer(async_file_appender.and(std::io::stderr)) + // .with_ansi(is_terminal) + // .with_timer(UtcTime::rfc_3339()) + // .with_target(false); + + + + + // match (json_format, timestamp) { + // (true, true) => builder.json().init(), + // (true, false) => builder.json().without_time().init(), + // (false, true) => builder.init(), + // (false, false) => builder.without_time().init(), + // } tracing::info!(%level, "Initialized tracing");