Skip to content

Commit

Permalink
feat(kv): Add styling for key in default format
Browse files Browse the repository at this point in the history
Make the key italic by default.
  • Loading branch information
tmccombs committed Mar 4, 2024
1 parent 9d26ad5 commit 9f4a33a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
14 changes: 11 additions & 3 deletions examples/direct_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
28 changes: 26 additions & 2 deletions src/fmt/kv.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::io::{self, Write};

use super::Formatter;
#[cfg(feature = "color")]
use super::WriteStyle;
use super::{Formatter, StyledValue};
#[cfg(feature = "color")]
use anstyle::Style;
use log::kv::{source::Source, Error, Key, Value, Visitor};

/// Format function for serializing key/value pairs
Expand Down Expand Up @@ -41,7 +45,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<Key<'k>> {
#[cfg(feature = "color")]
{
StyledValue {
style: if self.0.write_style == WriteStyle::Never {
Style::new()
} else {
Style::new().italic()
},
value: text,
}
}
#[cfg(not(feature = "color"))]
{
text
}
}
}
3 changes: 3 additions & 0 deletions src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ impl<T: std::fmt::Display> std::fmt::Display for StyledValue<T> {
}
}

#[cfg(not(feature = "color"))]
type StyledValue<T> = T;

/// The default format.
///
/// This format needs to work with any combination of crate features.
Expand Down
2 changes: 1 addition & 1 deletion src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: 'static>(&mut self, format: F) -> &mut Self
Expand Down

0 comments on commit 9f4a33a

Please sign in to comment.