From 74a576caf77e1e8bc64f471cdafcc84afd13c260 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 4 Oct 2025 10:14:22 +0000 Subject: [PATCH] Add option to toggle compact log format. Signed-off-by: Jason Volk --- src/core/config/mod.rs | 4 ++++ src/core/log/console.rs | 29 +++++++++++++++++------------ src/main/logging.rs | 2 +- tuwunel-example.toml | 4 ++++ 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/core/config/mod.rs b/src/core/config/mod.rs index 9037e3f1..5337b448 100644 --- a/src/core/config/mod.rs +++ b/src/core/config/mod.rs @@ -869,6 +869,10 @@ pub struct Config { #[serde(default = "true_fn", alias = "log_colours")] pub log_colors: bool, + /// Sets the log format to compact mode. + #[serde(default)] + pub log_compact: bool, + /// Configures the span events which will be outputted with the log. /// /// default: "none" diff --git a/src/core/log/console.rs b/src/core/log/console.rs index 308895aa..84cdc9e3 100644 --- a/src/core/log/console.rs +++ b/src/core/log/console.rs @@ -14,7 +14,7 @@ use tracing_subscriber::{ registry::LookupSpan, }; -use crate::{Config, Result, apply}; +use crate::{Config, Result, apply, is_equal_to}; static SYSTEMD_MODE: LazyLock = LazyLock::new(|| env::var("SYSTEMD_EXEC_PID").is_ok() && env::var("JOURNAL_STREAM").is_ok()); @@ -64,23 +64,16 @@ impl io::Write for &'_ ConsoleWriter { } pub struct ConsoleFormat { - _compact: Format, - full: Format, pretty: Format, + full: Format, + compact: Format, + compact_mode: bool, } impl ConsoleFormat { #[must_use] pub fn new(config: &Config) -> Self { Self { - _compact: fmt::format() - .compact() - .with_ansi(config.log_colors), - - full: Format::::default() - .with_thread_ids(config.log_thread_ids) - .with_ansi(config.log_colors), - pretty: fmt::format() .pretty() .with_ansi(config.log_colors) @@ -90,6 +83,16 @@ impl ConsoleFormat { .with_file(true) .with_line_number(true) .with_source_location(true), + + full: Format::::default() + .with_thread_ids(config.log_thread_ids) + .with_ansi(config.log_colors), + + compact: fmt::format() + .compact() + .with_ansi(config.log_colors), + + compact_mode: config.log_compact, } } } @@ -108,9 +111,11 @@ where let is_debug = cfg!(debug_assertions) && event .fields() - .any(|field| field.name() == "_debug"); + .map(|field| field.name()) + .any(is_equal_to!("_debug")); match *event.metadata().level() { + | _ if self.compact_mode => self.compact.format_event(ctx, writer, event), | Level::ERROR if !is_debug => self.pretty.format_event(ctx, writer, event), | _ => self.full.format_event(ctx, writer, event), } diff --git a/src/main/logging.rs b/src/main/logging.rs index daa52362..4e158548 100644 --- a/src/main/logging.rs +++ b/src/main/logging.rs @@ -33,8 +33,8 @@ pub(crate) fn init( .with_ansi(config.log_colors) .with_thread_ids(config.log_thread_ids) .with_span_events(console_span_events) - .event_format(ConsoleFormat::new(config)) .fmt_fields(ConsoleFormat::new(config)) + .event_format(ConsoleFormat::new(config)) .with_writer(ConsoleWriter::new(config)); let (console_reload_filter, console_reload_handle) = diff --git a/tuwunel-example.toml b/tuwunel-example.toml index fb051c5b..31aa653f 100644 --- a/tuwunel-example.toml +++ b/tuwunel-example.toml @@ -716,6 +716,10 @@ # #log_colors = true +# Sets the log format to compact mode. +# +#log_compact = false + # Configures the span events which will be outputted with the log. # #log_span_events = "none"