diff --git a/examples/direct_logger.rs b/examples/direct_logger.rs index 4d7f39d..471beb4 100644 --- a/examples/direct_logger.rs +++ b/examples/direct_logger.rs @@ -8,6 +8,8 @@ use env_logger::{Builder, WriteStyle}; use log::{Level, LevelFilter, Log, MetadataBuilder, Record}; +static kvs: (&str, &str) = ("test", "something"); + fn record() -> Record<'static> { let error_metadata = MetadataBuilder::new() .target("myApp") @@ -20,6 +22,7 @@ fn record() -> Record<'static> { .line(Some(433)) .file(Some("app.rs")) .module_path(Some("server")) + .key_values(&kvs) .build() } diff --git a/src/fmt/kv.rs b/src/fmt/kv.rs index bd2cae3..ecc362d 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 55108cb..ff32eb6 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.