Add option to toggle compact log format.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2025-10-04 10:14:22 +00:00
parent 2b7cf7d5d5
commit 74a576caf7
4 changed files with 26 additions and 13 deletions

View File

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

View File

@@ -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<bool> =
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<Compact>,
full: Format<Full>,
pretty: Format<Pretty>,
full: Format<Full>,
compact: Format<Compact>,
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::<Full>::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::<Full>::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),
}

View File

@@ -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) =

View File

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