diff --git a/README.md b/README.md index 01fd30875..c959ac56d 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ pub fn shave_the_yak(yak: &mut Yak) { break; } Err(err) => { - warn!(err:error = err; "Unable to locate a razor, retrying"); + warn!(err:err; "Unable to locate a razor, retrying"); } } } diff --git a/src/kv/mod.rs b/src/kv/mod.rs index 6dbfe6f66..1ccb82514 100644 --- a/src/kv/mod.rs +++ b/src/kv/mod.rs @@ -29,6 +29,15 @@ //! info!(a = 1; "Something of interest"); //! ``` //! +//! Key-values support the same shorthand identifer syntax as `format_args`: +//! +//! ``` +//! # use log::info; +//! let a = 1; +//! +//! info!(a; "Something of interest"); +//! ``` +//! //! Values are capturing using the [`ToValue`] trait by default. To capture a value //! using a different trait implementation, use a modifier after its key. Here's how //! the same example can capture `a` using its `Debug` implementation instead: @@ -44,7 +53,7 @@ //! - `:debug` will capture the value using `Debug`. //! - `:%` will capture the value using `Display`. //! - `:display` will capture the value using `Display`. -//! - `:error` will capture the value using `std::error::Error` (requires the `kv_std` feature). +//! - `:err` will capture the value using `std::error::Error` (requires the `kv_std` feature). //! - `:sval` will capture the value using `sval::Value` (requires the `kv_sval` feature). //! - `:serde` will capture the value using `serde::Serialize` (requires the `kv_serde` feature). //! diff --git a/src/kv/value.rs b/src/kv/value.rs index 96cb411d1..c7efa279d 100644 --- a/src/kv/value.rs +++ b/src/kv/value.rs @@ -1142,7 +1142,7 @@ macro_rules! as_display { /// Get a value from an error. #[cfg(feature = "kv_unstable_std")] -#[deprecated(note = "use the `key:error = value` macro syntax instead")] +#[deprecated(note = "use the `key:err = value` macro syntax instead")] #[macro_export] macro_rules! as_error { ($capture:expr) => { diff --git a/src/lib.rs b/src/lib.rs index feb14e115..c1f88fe8d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -112,7 +112,7 @@ //! break; //! } //! Err(err) => { -//! warn!(err:error = err; "Unable to locate a razor, retrying"); +//! warn!(err:err; "Unable to locate a razor, retrying"); //! } //! } //! } diff --git a/src/macros.rs b/src/macros.rs index 9ad6e7f08..ccccd5bd6 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -30,7 +30,7 @@ #[macro_export] macro_rules! log { // log!(target: "my_target", Level::Info, key1:? = 42, key2 = true; "a {} event", "log"); - (target: $target:expr, $lvl:expr, $($key:tt $(:$capture:tt)? = $value:expr),+; $($arg:tt)+) => ({ + (target: $target:expr, $lvl:expr, $($key:tt $(:$capture:tt)? $(= $value:expr)?),+; $($arg:tt)+) => ({ let lvl = $lvl; if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() { $crate::__private_api::log::<&_>( @@ -38,7 +38,7 @@ macro_rules! log { lvl, &($target, $crate::__private_api::module_path!(), $crate::__private_api::file!()), $crate::__private_api::line!(), - &[$(($crate::__log_key!($key), $crate::__log_value!(($value)$(:$capture)*))),+] + &[$(($crate::__log_key!($key), $crate::__log_value!($key $(:$capture)* = $($value)*))),+] ); } }); @@ -256,10 +256,19 @@ macro_rules! __log_key { #[macro_export] #[cfg(feature = "kv")] macro_rules! __log_value { - // Default - (($args:expr)) => { + // Entrypoint + ($key:tt = $args:expr) => { $crate::__log_value!(($args):value) }; + ($key:tt :$capture:tt = $args:expr) => { + $crate::__log_value!(($args):$capture) + }; + ($key:ident =) => { + $crate::__log_value!(($key):value) + }; + ($key:ident :$capture:tt =) => { + $crate::__log_value!(($key):$capture) + }; // ToValue (($args:expr):value) => { $crate::__private_api::capture_to_value(&&$args) diff --git a/tests/macros.rs b/tests/macros.rs index 07bdf53f0..20da6ac44 100644 --- a/tests/macros.rs +++ b/tests/macros.rs @@ -165,6 +165,15 @@ fn kv_named_args() { all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world"); } +#[test] +#[cfg(feature = "kv")] +fn kv_ident() { + let cat_1 = "chashu"; + let cat_2 = "nori"; + + all_log_macros!(cat_1, cat_2:%, cat_count = 2; "hello {world}", world = "world"); +} + #[test] #[cfg(feature = "kv")] fn kv_expr_context() { @@ -274,7 +283,7 @@ fn kv_display() { #[cfg(feature = "kv_std")] fn kv_error() { all_log_macros!( - a:error = std::io::Error::new(std::io::ErrorKind::Other, "an error"); + a:err = std::io::Error::new(std::io::ErrorKind::Other, "an error"); "hello world" ); }