Skip to content

Commit 9f4a33a

Browse files
committed
feat(kv): Add styling for key in default format
Make the key italic by default.
1 parent 9d26ad5 commit 9f4a33a

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

examples/direct_logger.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,27 @@ use env_logger::{Builder, WriteStyle};
88

99
use log::{Level, LevelFilter, Log, MetadataBuilder, Record};
1010

11+
#[cfg(feature = "unstable-kv")]
12+
static KVS: (&str, &str) = ("test", "something");
13+
1114
fn record() -> Record<'static> {
1215
let error_metadata = MetadataBuilder::new()
1316
.target("myApp")
1417
.level(Level::Error)
1518
.build();
1619

17-
Record::builder()
20+
let mut builder = Record::builder();
21+
builder
1822
.metadata(error_metadata)
1923
.args(format_args!("Error!"))
2024
.line(Some(433))
2125
.file(Some("app.rs"))
22-
.module_path(Some("server"))
23-
.build()
26+
.module_path(Some("server"));
27+
#[cfg(feature = "unstable-kv")]
28+
{
29+
builder.key_values(&KVS);
30+
}
31+
builder.build()
2432
}
2533

2634
fn main() {

src/fmt/kv.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use std::io::{self, Write};
22

3-
use super::Formatter;
3+
#[cfg(feature = "color")]
4+
use super::WriteStyle;
5+
use super::{Formatter, StyledValue};
6+
#[cfg(feature = "color")]
7+
use anstyle::Style;
48
use log::kv::{source::Source, Error, Key, Value, Visitor};
59

610
/// Format function for serializing key/value pairs
@@ -41,7 +45,27 @@ impl<'a, 'kvs> Visitor<'kvs> for DefaultVisitor<'a> {
4145
fn visit_pair(&mut self, key: Key, value: Value<'kvs>) -> Result<(), Error> {
4246
// TODO: add styling
4347
// tracing-subscriber uses italic for the key and dimmed for the =
44-
write!(self.0, " {}={}", key, value)?;
48+
write!(self.0, " {}={}", self.style_key(key), value)?;
4549
Ok(())
4650
}
4751
}
52+
53+
impl DefaultVisitor<'_> {
54+
fn style_key<'k>(&self, text: Key<'k>) -> StyledValue<Key<'k>> {
55+
#[cfg(feature = "color")]
56+
{
57+
StyledValue {
58+
style: if self.0.write_style == WriteStyle::Never {
59+
Style::new()
60+
} else {
61+
Style::new().italic()
62+
},
63+
value: text,
64+
}
65+
}
66+
#[cfg(not(feature = "color"))]
67+
{
68+
text
69+
}
70+
}
71+
}

src/fmt/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ impl<T: std::fmt::Display> std::fmt::Display for StyledValue<T> {
292292
}
293293
}
294294

295+
#[cfg(not(feature = "color"))]
296+
type StyledValue<T> = T;
297+
295298
/// The default format.
296299
///
297300
/// This format needs to work with any combination of crate features.

src/logger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ impl Builder {
321321
/// The format function is expected to output the string directly to the `Formatter` so that
322322
/// implementations can use the [`std::fmt`] macros, similar to the main format function.
323323
///
324-
/// The default format uses a space to separte each key-value pair, with an "=" between
324+
/// The default format uses a space to separate each key-value pair, with an "=" between
325325
/// the key and value.
326326
#[cfg(feature = "unstable-kv")]
327327
pub fn format_key_values<F: 'static>(&mut self, format: F) -> &mut Self

0 commit comments

Comments
 (0)