From f6e2d45615057917b11dfc140b0be325b7ffd1b0 Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Mon, 4 Mar 2024 00:38:09 -0700 Subject: [PATCH] feat(kv): Use now-stable kv feature of log crate Which means updating to 0.4.21 --- Cargo.lock | 13 ++----------- Cargo.toml | 4 ++-- src/fmt/kv.rs | 12 +++++------- src/fmt/mod.rs | 3 +++ src/logger.rs | 2 +- 5 files changed, 13 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 39eb55d..02d00c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -92,12 +92,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "log" -version = "0.4.20" +version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" -dependencies = [ - "value-bag", -] +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" [[package]] name = "memchr" @@ -128,12 +125,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" -[[package]] -name = "value-bag" -version = "1.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cdbaf5e132e593e9fc1de6a15bbec912395b11fb9719e061cf64f804524c503" - [[package]] name = "windows-sys" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index beb4a7f..78effb6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/fmt/kv.rs b/src/fmt/kv.rs index cc8cf4d..5d8cfca 100644 --- a/src/fmt/kv.rs +++ b/src/fmt/kv.rs @@ -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 /// @@ -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> { #[cfg(feature = "color")] { diff --git a/src/fmt/mod.rs b/src/fmt/mod.rs index cbacc44..f18940a 100644 --- a/src/fmt/mod.rs +++ b/src/fmt/mod.rs @@ -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 . diff --git a/src/logger.rs b/src/logger.rs index ccc3615..6f41490 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -326,7 +326,7 @@ impl Builder { #[cfg(feature = "unstable-kv")] pub fn format_key_values(&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