From 1e4acc80e0d05f9f83061ba268a7732b47c5807f Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Sat, 27 Jan 2024 01:50:06 -0700 Subject: [PATCH] feat(kv): Add styling for key in default format Make the key italic by default. --- examples/direct_logger.rs | 14 +++++++++++--- src/fmt/kv.rs | 25 +++++++++++++++++++++++-- src/fmt/mod.rs | 3 +++ src/logger.rs | 2 +- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/examples/direct_logger.rs b/examples/direct_logger.rs index 4d7f39d0..397ccd8c 100644 --- a/examples/direct_logger.rs +++ b/examples/direct_logger.rs @@ -8,19 +8,27 @@ use env_logger::{Builder, WriteStyle}; use log::{Level, LevelFilter, Log, MetadataBuilder, Record}; +#[cfg(feature = "unstable-kv")] +static KVS: (&str, &str) = ("test", "something"); + fn record() -> Record<'static> { let error_metadata = MetadataBuilder::new() .target("myApp") .level(Level::Error) .build(); - Record::builder() + let mut builder = Record::builder(); + builder .metadata(error_metadata) .args(format_args!("Error!")) .line(Some(433)) .file(Some("app.rs")) - .module_path(Some("server")) - .build() + .module_path(Some("server")); + #[cfg(feature = "unstable-kv")] + { + builder.key_values(&KVS); + } + builder.build() } fn main() { diff --git a/src/fmt/kv.rs b/src/fmt/kv.rs index bd2cae30..ecc362da 100644 --- a/src/fmt/kv.rs +++ b/src/fmt/kv.rs @@ -1,6 +1,7 @@ use std::io::{self, Write}; -use super::Formatter; +use super::{Formatter, StyledValue, WriteStyle}; +use anstyle::Style; use log::kv::{source::Source, Error, Key, Value, Visitor}; /// Format function for serializing key/value pairs @@ -41,7 +42,27 @@ impl<'a, 'kvs> Visitor<'kvs> for DefaultVisitor<'a> { fn visit_pair(&mut self, key: Key, value: Value<'kvs>) -> Result<(), Error> { // TODO: add styling // tracing-subscriber uses italic for the key and dimmed for the = - write!(self.0, " {}={}", key, value)?; + write!(self.0, " {}={}", self.style_key(key), value)?; Ok(()) } } + +impl DefaultVisitor<'_> { + fn style_key<'k>(&self, text: Key<'k>) -> StyledValue> { + #[cfg(feature = "color")] + { + StyledValue { + style: if self.0.write_style == WriteStyle::Never { + Style::new() + } else { + Style::new().italic() + }, + value: text, + } + } + #[cfg(not(feature = "color"))] + { + key + } + } +} diff --git a/src/fmt/mod.rs b/src/fmt/mod.rs index 55108cb2..ff32eb6a 100644 --- a/src/fmt/mod.rs +++ b/src/fmt/mod.rs @@ -292,6 +292,9 @@ impl std::fmt::Display for StyledValue { } } +#[cfg(not(feature = "color"))] +type StyledValue = T; + /// The default format. /// /// This format needs to work with any combination of crate features. diff --git a/src/logger.rs b/src/logger.rs index 509c299b..ccc36154 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -321,7 +321,7 @@ impl Builder { /// The format function is expected to output the string directly to the `Formatter` so that /// implementations can use the [`std::fmt`] macros, similar to the main format function. /// - /// The default format uses a space to separte each key-value pair, with an "=" between + /// The default format uses a space to separate each key-value pair, with an "=" between /// the key and value. #[cfg(feature = "unstable-kv")] pub fn format_key_values(&mut self, format: F) -> &mut Self