Skip to content

Commit

Permalink
feat(kv): Use now-stable kv feature of log crate
Browse files Browse the repository at this point in the history
Which means updating to 0.4.21
  • Loading branch information
tmccombs committed Mar 5, 2024
1 parent 9f4a33a commit f6e2d45
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 21 deletions.
13 changes: 2 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ color = ["dep:anstream", "dep:anstyle"]
auto-color = ["color", "anstream/auto"]
humantime = ["dep:humantime"]
regex = ["env_filter/regex"]
unstable-kv = ["log/kv_unstable"]
unstable-kv = ["log/kv"]

[dependencies]
log = { version = "0.4.8", features = ["std"] }
log = { version = "0.4.21", features = ["std"] }
env_filter = { version = "0.1.0", path = "crates/env_filter", default-features = false }
humantime = { version = "2.0.0", optional = true }
anstream = { version = "0.6.11", default-features = false, features = ["wincon"], optional = true }
Expand Down
12 changes: 5 additions & 7 deletions src/fmt/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::WriteStyle;
use super::{Formatter, StyledValue};
#[cfg(feature = "color")]
use anstyle::Style;
use log::kv::{source::Source, Error, Key, Value, Visitor};
use log::kv::{Error, Key, Source, Value, VisitSource};

/// Format function for serializing key/value pairs
///
Expand Down Expand Up @@ -35,22 +35,20 @@ pub fn hidden_kv_format(_formatter: &mut Formatter, _fields: &dyn Source) -> io:
/// For example: `ip=127.0.0.1 port=123456 path=/example`
pub fn default_kv_format(formatter: &mut Formatter, fields: &dyn Source) -> io::Result<()> {
fields
.visit(&mut DefaultVisitor(formatter))
.visit(&mut DefaultVisitSource(formatter))
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
}

struct DefaultVisitor<'a>(&'a mut Formatter);
struct DefaultVisitSource<'a>(&'a mut Formatter);

impl<'a, 'kvs> Visitor<'kvs> for DefaultVisitor<'a> {
impl<'a, 'kvs> VisitSource<'kvs> for DefaultVisitSource<'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, " {}={}", self.style_key(key), value)?;
Ok(())
}
}

impl DefaultVisitor<'_> {
impl DefaultVisitSource<'_> {
fn style_key<'k>(&self, text: Key<'k>) -> StyledValue<Key<'k>> {
#[cfg(feature = "color")]
{
Expand Down
3 changes: 3 additions & 0 deletions src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@
//! function that is called by the default format with [`Builder::format_key_values`].
//!
//! ```
//! # #[cfg(feature= "unstable-kv")]
//! # {
//! use log::info;
//! env_logger::init();
//! info!(x="45"; "Some message");
//! info!(x="12"; "Another message {x}", x="12");
//! # }
//! ```
//!
//! See <https://docs.rs/log/latest/log/#structured-logging>.
Expand Down
2 changes: 1 addition & 1 deletion src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl Builder {
#[cfg(feature = "unstable-kv")]
pub fn format_key_values<F: 'static>(&mut self, format: F) -> &mut Self
where
F: Fn(&mut Formatter, &dyn log::kv::source::Source) -> io::Result<()> + Sync + Send,
F: Fn(&mut Formatter, &dyn log::kv::Source) -> io::Result<()> + Sync + Send,
{
self.format.kv_format = Some(Box::new(format));
self
Expand Down

0 comments on commit f6e2d45

Please sign in to comment.