From 62c6607b259106b50be846fdfdb527e214817920 Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Tue, 7 Dec 2021 22:48:47 +1000 Subject: [PATCH 01/10] allow macros to be more consistent with kvs --- Cargo.toml | 1 + README.md | 12 ++- src/kv/value.rs | 33 ++++++++ src/lib.rs | 40 +++++++++- src/macros.rs | 106 ++++++++++++++++--------- tests/Cargo.toml | 3 + tests/macros.rs | 200 +++++++++++++++++++++++++++++++++++++++++------ 7 files changed, 331 insertions(+), 64 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a8bf0abb9..5a67bdce6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,6 +59,7 @@ sval = { version = "=1.0.0-alpha.5", optional = true, default-features = false } value-bag = { version = "=1.0.0-alpha.8", optional = true, default-features = false } [dev-dependencies] +rustversion = "1.0" serde = { version = "1.0", features = ["derive"] } serde_test = "1.0" sval = { version = "=1.0.0-alpha.5", features = ["derive"] } diff --git a/README.md b/README.md index 8a7c1eea3..c17b6486e 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ This version is explicitly tested in CI and may be bumped in any release as need ## Usage -## In libraries +### In libraries Libraries should link only to the `log` crate, and use the provided macros to log whatever information will be useful to downstream consumers: @@ -55,7 +55,7 @@ pub fn shave_the_yak(yak: &mut Yak) { } ``` -## In executables +### In executables In order to produce log output, executables have to use a logger implementation compatible with the facade. There are many available implementations to choose from, here are some of the most popular ones: @@ -87,3 +87,11 @@ function to do this. Any log messages generated before the logger is initialized will be ignored. The executable itself may use the `log` crate to log as well. + +## Structured logging + +If you enable the `kv_unstable` feature, you can associate structured data with your log records: + +```rust + +``` diff --git a/src/kv/value.rs b/src/kv/value.rs index 4cbbca583..727c249ea 100644 --- a/src/kv/value.rs +++ b/src/kv/value.rs @@ -37,6 +37,39 @@ impl<'v> ToValue for Value<'v> { } } +/// Get a value from a type implementing `std::fmt::Debug`. +#[macro_export] +macro_rules! as_debug { + ($capture:expr) => ($crate::kv::Value::from_debug(&$capture)); +} + +/// Get a value from a type implementing `std::fmt::Display`. +#[macro_export] +macro_rules! as_display { + ($capture:expr) => ($crate::kv::Value::from_display(&$capture)); +} + +/// Get a value from an error. +#[cfg(feature = "kv_unstable_std")] +#[macro_export] +macro_rules! as_error { + ($capture:expr) => ($crate::kv::Value::from_dyn_error(&$capture)); +} + +#[cfg(feature = "kv_unstable_serde")] +/// Get a value from a type implementing `serde::Serialize`. +#[macro_export] +macro_rules! as_serde { + ($capture:expr) => ($crate::kv::Value::from_serde(&$capture)); +} + +/// Get a value from a type implementing `sval::value::Value`. +#[cfg(feature = "kv_unstable_sval")] +#[macro_export] +macro_rules! as_sval { + ($capture:expr) => ($crate::kv::Value::from_sval(&$capture)); +} + /// A value in a structured key-value pair. /// /// # Capturing values diff --git a/src/lib.rs b/src/lib.rs index 58c636b92..4123a712c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,7 +24,7 @@ //! though that default may be overridden. Logger implementations typically use //! the target to filter requests based on some user configuration. //! -//! # Use +//! # Usage //! //! The basic use of the log crate is through the five logging macros: [`error!`], //! [`warn!`], [`info!`], [`debug!`] and [`trace!`] @@ -55,7 +55,7 @@ //! use log::{info, warn}; //! //! pub fn shave_the_yak(yak: &mut Yak) { -//! info!(target: "yak_events", "Commencing yak shaving for {:?}", yak); +//! info!(target = "yak_events"; "Commencing yak shaving for {:?}", yak); //! //! loop { //! match find_a_razor() { @@ -86,6 +86,42 @@ //! //! The logging system may only be initialized once. //! +//! ## Structured logging +//! +//! If you enable the `kv_unstable` feature you can associate structured values +//! with your log records. If we take the example from before, we can include +//! some additional context besides what's in the formatted message: +//! +//! ```edition2018 +//! # #[macro_use] extern crate serde; +//! # #[derive(Debug, Serialize)] pub struct Yak(String); +//! # impl Yak { fn shave(&mut self, _: u32) {} } +//! # fn find_a_razor() -> Result { Ok(1) } +//! # #[cfg(feature = "kv_unstable_serde")] +//! # fn main() { +//! use log::{info, warn, as_serde, as_error}; +//! +//! pub fn shave_the_yak(yak: &mut Yak) { +//! info!(target = "yak_events"; yak = as_serde!(yak); "Commencing yak shaving"); +//! +//! loop { +//! match find_a_razor() { +//! Ok(razor) => { +//! info!(razor = razor; "Razor located"); +//! yak.shave(razor); +//! break; +//! } +//! Err(err) => { +//! warn!(err = as_error!(err); "Unable to locate a razor, retrying"); +//! } +//! } +//! } +//! } +//! # } +//! # #[cfg(not(feature = "kv_unstable_serde"))] +//! # fn main() {} +//! ``` +//! //! # Available logging implementations //! //! In order to produce log output executables have to use diff --git a/src/macros.rs b/src/macros.rs index a90feef34..24331cc26 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -29,18 +29,21 @@ /// ``` #[macro_export(local_inner_macros)] macro_rules! log { - (target: $target:expr, $lvl:expr, $($key:ident = $value:expr),* ; $fmt:expr, $($arg:tt)+) => ({ + // log!(target = "my_target", Level::Info; key1 = 42, key2 = true; "a {} event", "log"); + (target = $target:expr, $lvl:expr; $($key:ident = $value:expr),+; $($arg:tt)+) => ({ let lvl = $lvl; if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() { $crate::__private_api_log( - __log_format_args!($fmt, $($arg)+), + __log_format_args!($($arg)+), lvl, &($target, __log_module_path!(), __log_file!(), __log_line!()), - Some(&[$((__log_stringify!($key), &$value)),*]) + Some(&[$((__log_stringify!($key), &$value)),+]) ); } }); - (target: $target:expr, $lvl:expr, $($arg:tt)+) => ({ + + // log!(target = "my_target", Level::Info; "a {} event", "log"); + (target = $target:expr, $lvl:expr; $($arg:tt)+) => ({ let lvl = $lvl; if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() { $crate::__private_api_log( @@ -51,7 +54,18 @@ macro_rules! log { ); } }); - ($lvl:expr, $($arg:tt)+) => (log!(target: __log_module_path!(), $lvl, $($arg)+)) + + // log!(target: "my_target", Level::Info, "a log event") + (target: $target:expr, $lvl:expr, $($arg:tt)+) => (log!(target = $target, $lvl; $($arg)+)); + + // log!(target = "my_target", Level::Info; "a log event") + (target = $target:expr, $lvl:expr; $($arg:tt)+) => (log!(target = $target, $lvl; $($arg)+)); + + // log!(Level::Info, "a log event") + ($lvl:expr, $($arg:tt)+) => (log!(target = __log_module_path!(), $lvl; $($arg)+)); + + // log!(Level::Info; "a log event") + ($lvl:expr; $($arg:tt)+) => (log!(target = __log_module_path!(), $lvl; $($arg)+)) } /// Logs a message at the error level. @@ -70,12 +84,15 @@ macro_rules! log { /// ``` #[macro_export(local_inner_macros)] macro_rules! error { - (target: $target:expr, $($arg:tt)+) => ( - log!(target: $target, $crate::Level::Error, $($arg)+) - ); - ($($arg:tt)+) => ( - log!($crate::Level::Error, $($arg)+) - ) + // error!(target = "my_target"; key1 = 42, key2 = true; "a {} event", "log") + // error!(target = "my_target"; "a {} event", "log") + (target = $target:expr; $($arg:tt)+) => (log!(target = $target, $crate::Level::Error; $($arg)+)); + + // error!(target: "my_target", "a {} event", "log") + (target: $target:expr, $($arg:tt)+) => (log!(target = $target, $crate::Level::Error; $($arg)+)); + + // error!("a {} event", "log") + ($($arg:tt)+) => (log!($crate::Level::Error; $($arg)+)) } /// Logs a message at the warn level. @@ -94,12 +111,15 @@ macro_rules! error { /// ``` #[macro_export(local_inner_macros)] macro_rules! warn { - (target: $target:expr, $($arg:tt)+) => ( - log!(target: $target, $crate::Level::Warn, $($arg)+) - ); - ($($arg:tt)+) => ( - log!($crate::Level::Warn, $($arg)+) - ) + // warn!(target = "my_target"; key1 = 42, key2 = true; "a {} event", "log") + // warn!(target = "my_target"; "a {} event", "log") + (target = $target:expr; $($arg:tt)+) => (log!(target = $target, $crate::Level::Warn; $($arg)+)); + + // warn!(target: "my_target", "a {} event", "log") + (target: $target:expr, $($arg:tt)+) => (log!(target = $target, $crate::Level::Warn; $($arg)+)); + + // warn!("a {} event", "log") + ($($arg:tt)+) => (log!($crate::Level::Warn; $($arg)+)) } /// Logs a message at the info level. @@ -120,12 +140,15 @@ macro_rules! warn { /// ``` #[macro_export(local_inner_macros)] macro_rules! info { - (target: $target:expr, $($arg:tt)+) => ( - log!(target: $target, $crate::Level::Info, $($arg)+) - ); - ($($arg:tt)+) => ( - log!($crate::Level::Info, $($arg)+) - ) + // info!(target = "my_target"; key1 = 42, key2 = true; "a {} event", "log") + // info!(target = "my_target"; "a {} event", "log") + (target = $target:expr; $($arg:tt)+) => (log!(target = $target, $crate::Level::Info; $($arg)+)); + + // info!(target: "my_target", "a {} event", "log") + (target: $target:expr, $($arg:tt)+) => (log!(target = $target, $crate::Level::Info; $($arg)+)); + + // info!("a {} event", "log") + ($($arg:tt)+) => (log!($crate::Level::Info; $($arg)+)) } /// Logs a message at the debug level. @@ -145,12 +168,15 @@ macro_rules! info { /// ``` #[macro_export(local_inner_macros)] macro_rules! debug { - (target: $target:expr, $($arg:tt)+) => ( - log!(target: $target, $crate::Level::Debug, $($arg)+) - ); - ($($arg:tt)+) => ( - log!($crate::Level::Debug, $($arg)+) - ) + // debug!(target = "my_target"; key1 = 42, key2 = true; "a {} event", "log") + // debug!(target = "my_target"; "a {} event", "log") + (target = $target:expr; $($arg:tt)+) => (log!(target = $target, $crate::Level::Debug; $($arg)+)); + + // debug!(target: "my_target", "a {} event", "log") + (target: $target:expr, $($arg:tt)+) => (log!(target = $target, $crate::Level::Debug; $($arg)+)); + + // debug!("a {} event", "log") + ($($arg:tt)+) => (log!($crate::Level::Debug; $($arg)+)) } /// Logs a message at the trace level. @@ -172,12 +198,15 @@ macro_rules! debug { /// ``` #[macro_export(local_inner_macros)] macro_rules! trace { - (target: $target:expr, $($arg:tt)+) => ( - log!(target: $target, $crate::Level::Trace, $($arg)+) - ); - ($($arg:tt)+) => ( - log!($crate::Level::Trace, $($arg)+) - ) + // trace!(target = "my_target"; key1 = 42, key2 = true; "a {} event", "log") + // trace!(target = "my_target"; "a {} event", "log") + (target = $target:expr; $($arg:tt)+) => (log!(target = $target, $crate::Level::Trace; $($arg)+)); + + // trace!(target: "my_target", "a {} event", "log") + (target: $target:expr, $($arg:tt)+) => (log!(target = $target, $crate::Level::Trace; $($arg)+)); + + // trace!("a {} event", "log") + ($($arg:tt)+) => (log!($crate::Level::Trace; $($arg)+)) } /// Determines if a message logged at the specified level in that module will @@ -208,14 +237,17 @@ macro_rules! trace { /// ``` #[macro_export(local_inner_macros)] macro_rules! log_enabled { - (target: $target:expr, $lvl:expr) => {{ + (target = $target:expr, $lvl:expr) => {{ let lvl = $lvl; lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() && $crate::__private_api_enabled(lvl, $target) }}; + (target: $target:expr, $lvl:expr) => { + log_enabled!(target = $target, $lvl) + }; ($lvl:expr) => { - log_enabled!(target: __log_module_path!(), $lvl) + log_enabled!(target = __log_module_path!(), $lvl) }; } diff --git a/tests/Cargo.toml b/tests/Cargo.toml index cb6b8600f..25ac12bec 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -9,3 +9,6 @@ std = ["log/std"] [dependencies.log] path = ".." + +[dev-dependencies.rustversion] +version = "1.0" diff --git a/tests/macros.rs b/tests/macros.rs index 0ccb64028..cd0e44cf7 100644 --- a/tests/macros.rs +++ b/tests/macros.rs @@ -2,46 +2,200 @@ #[macro_use] extern crate log; +macro_rules! all_log_macros { + ($($arg:tt)*) => ({ + trace!($($arg)*); + debug!($($arg)*); + info!($($arg)*); + warn!($($arg)*); + error!($($arg)*); + }); +} + #[test] -fn base() { - info!("hello"); - info!("hello",); +fn no_args() { + for lvl in log::Level::iter() { + log!(lvl, "hello"); + log!(lvl, "hello",); + + log!(target: "my_target", lvl, "hello"); + log!(target: "my_target", lvl, "hello",); + + log!(lvl; "hello"); + log!(lvl; "hello",); + + log!(target = "my_target", lvl; "hello"); + log!(target = "my_target", lvl; "hello",); + } + + all_log_macros!("hello"); + all_log_macros!("hello",); + + all_log_macros!(target = "my_target"; "hello"); + all_log_macros!(target = "my_target"; "hello",); + + all_log_macros!(target: "my_target", "hello"); + all_log_macros!(target: "my_target", "hello",); } #[test] -fn base_expr_context() { - let _ = info!("hello"); +fn anonymous_args() { + for lvl in log::Level::iter() { + log!(lvl, "hello {}", "world"); + log!(lvl, "hello {}", "world",); + + log!(target: "my_target", lvl, "hello {}", "world"); + log!(target: "my_target", lvl, "hello {}", "world",); + + log!(lvl; "hello {}", "world"); + log!(lvl; "hello {}", "world",); + + log!(target = "my_target", lvl; "hello {}", "world"); + log!(target = "my_target", lvl; "hello {}", "world",); + } + + all_log_macros!("hello {}", "world"); + all_log_macros!("hello {}", "world",); + + all_log_macros!(target = "my_target"; "hello {}", "world"); + all_log_macros!(target = "my_target"; "hello {}", "world",); + + all_log_macros!(target: "my_target", "hello {}", "world"); + all_log_macros!(target: "my_target", "hello {}", "world",); } #[test] -fn with_args() { - info!("hello {}", "cats"); - info!("hello {}", "cats",); - info!("hello {}", "cats",); +fn named_args() { + for lvl in log::Level::iter() { + log!(lvl, "hello {world}", world = "world"); + log!(lvl, "hello {world}", world = "world",); + + log!(target: "my_target", lvl, "hello {world}", world = "world"); + log!(target: "my_target", lvl, "hello {world}", world = "world",); + + log!(lvl; "hello {world}", world = "world"); + log!(lvl; "hello {world}", world = "world",); + + log!(target = "my_target", lvl; "hello {world}", world = "world"); + log!(target = "my_target", lvl; "hello {world}", world = "world",); + } + + all_log_macros!("hello {world}", world = "world"); + all_log_macros!("hello {world}", world = "world",); + + all_log_macros!(target = "my_target"; "hello {world}", world = "world"); + all_log_macros!(target = "my_target"; "hello {world}", world = "world",); + + all_log_macros!(target: "my_target", "hello {world}", world = "world"); + all_log_macros!(target: "my_target", "hello {world}", world = "world",); } #[test] -fn with_args_expr_context() { - match "cats" { - cats => info!("hello {}", cats), - }; +#[rustversion::since(1.58)] +fn implicit_named_args() { + let world = "world"; + + for lvl in log::Level::iter() { + log!(lvl, "hello {world}"); + log!(lvl, "hello {world}",); + + log!(target: "my_target", lvl, "hello {world}"); + log!(target: "my_target", lvl, "hello {world}",); + + log!(lvl; "hello {world}"); + log!(lvl; "hello {world}",); + + log!(target = "my_target", lvl; "hello {world}"); + log!(target = "my_target", lvl; "hello {world}",); + } + + all_log_macros!("hello {world}"); + all_log_macros!("hello {world}",); + + all_log_macros!(target = "my_target"; "hello {world}"); + all_log_macros!(target = "my_target"; "hello {world}",); + + all_log_macros!(target: "my_target", "hello {world}"); + all_log_macros!(target: "my_target", "hello {world}",); +} + +#[test] +fn enabled() { + for lvl in log::Level::iter() { + let _enabled = if log_enabled!(target: "my_target", lvl) { + true + } else { + false + }; + + let _enabled = if log_enabled!(target = "my_target", lvl) { + true + } else { + false + }; + } +} + +#[test] +fn expr() { + for lvl in log::Level::iter() { + let _ = log!(lvl; "hello"); + } +} + +#[test] +#[cfg(feature = "kv_unstable")] +fn kv_no_args() { + for lvl in log::Level::iter() { + log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori"; "hello"); + + log!(lvl; cat_1 = "chashu", cat_2 = "nori"; "hello"); + } + + all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori"; "hello"); + all_log_macros!(cat_1 = "chashu", cat_2 = "nori"; "hello"); } #[test] -fn with_named_args() { - let cats = "cats"; +#[cfg(feature = "kv_unstable")] +fn kv_anonymous_args() { + for lvl in log::Level::iter() { + log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori"; "hello {}", "world"); + + log!(lvl; cat_1 = "chashu", cat_2 = "nori"; "hello {}", "world"); + } - info!("hello {cats}", cats = cats); - info!("hello {cats}", cats = cats,); - info!("hello {cats}", cats = cats,); + all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori"; "hello {}", "world"); + all_log_macros!(cat_1 = "chashu", cat_2 = "nori"; "hello {}", "world"); } #[test] #[cfg(feature = "kv_unstable")] -fn kv() { - info!(cat_1 = "chashu", cat_2 = "nori"; "hello {}", "cats"); - info!(target: "my_target", cat_1 = "chashu", cat_2 = "nori"; "hello {}", "cats"); - log!(target: "my_target", log::Level::Warn, cat_1 = "chashu", cat_2 = "nori"; "hello {}", "cats"); +fn kv_named_args() { + for lvl in log::Level::iter() { + log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori"; "hello {world}", world = "world"); + + log!(lvl; cat_1 = "chashu", cat_2 = "nori"; "hello {world}", world = "world"); + } + + all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori"; "hello {world}", world = "world"); + all_log_macros!(cat_1 = "chashu", cat_2 = "nori"; "hello {world}", world = "world"); +} + +#[test] +#[cfg(feature = "kv_unstable")] +#[rustversion::since(1.58)] +fn kv_implicit_named_args() { + let world = "world"; + + for lvl in log::Level::iter() { + log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori"; "hello {world}"); + + log!(lvl; cat_1 = "chashu", cat_2 = "nori"; "hello {world}"); + } + + all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori"; "hello {world}"); + all_log_macros!(cat_1 = "chashu", cat_2 = "nori"; "hello {world}"); } #[test] @@ -49,7 +203,7 @@ fn kv() { fn kv_expr_context() { match "chashu" { cat_1 => { - info!(target: "target", cat_1 = cat_1, cat_2 = "nori"; "hello {}", "cats") + info!(target = "target"; cat_1 = cat_1, cat_2 = "nori"; "hello {}", "cats") } }; } From bbc5527da69adbb32bf5224552b14f9945542938 Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Tue, 7 Dec 2021 22:58:27 +1000 Subject: [PATCH 02/10] fill in readme example --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index c17b6486e..c67207f1a 100644 --- a/README.md +++ b/README.md @@ -93,5 +93,22 @@ The executable itself may use the `log` crate to log as well. If you enable the `kv_unstable` feature, you can associate structured data with your log records: ```rust +use log::{info, trace, warn, as_serde, as_error}; +pub fn shave_the_yak(yak: &mut Yak) { + trace!(target = "yak_events"; yak = as_serde!(yak); "Commencing yak shaving"); + + loop { + match find_a_razor() { + Ok(razor) => { + info!(razor = razor; "Razor located"); + yak.shave(razor); + break; + } + Err(err) => { + warn!(err = as_error!(err); "Unable to locate a razor, retrying"); + } + } + } +} ``` From 77667f5436eaf507dadad0c3ba523c45354c72e4 Mon Sep 17 00:00:00 2001 From: KodrAus Date: Sun, 12 Dec 2021 10:57:45 +1000 Subject: [PATCH 03/10] move implicit arg tests into module --- tests/macros.rs | 100 ++++++++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 45 deletions(-) diff --git a/tests/macros.rs b/tests/macros.rs index cd0e44cf7..dcc88f851 100644 --- a/tests/macros.rs +++ b/tests/macros.rs @@ -90,35 +90,6 @@ fn named_args() { all_log_macros!(target: "my_target", "hello {world}", world = "world",); } -#[test] -#[rustversion::since(1.58)] -fn implicit_named_args() { - let world = "world"; - - for lvl in log::Level::iter() { - log!(lvl, "hello {world}"); - log!(lvl, "hello {world}",); - - log!(target: "my_target", lvl, "hello {world}"); - log!(target: "my_target", lvl, "hello {world}",); - - log!(lvl; "hello {world}"); - log!(lvl; "hello {world}",); - - log!(target = "my_target", lvl; "hello {world}"); - log!(target = "my_target", lvl; "hello {world}",); - } - - all_log_macros!("hello {world}"); - all_log_macros!("hello {world}",); - - all_log_macros!(target = "my_target"; "hello {world}"); - all_log_macros!(target = "my_target"; "hello {world}",); - - all_log_macros!(target: "my_target", "hello {world}"); - all_log_macros!(target: "my_target", "hello {world}",); -} - #[test] fn enabled() { for lvl in log::Level::iter() { @@ -182,22 +153,6 @@ fn kv_named_args() { all_log_macros!(cat_1 = "chashu", cat_2 = "nori"; "hello {world}", world = "world"); } -#[test] -#[cfg(feature = "kv_unstable")] -#[rustversion::since(1.58)] -fn kv_implicit_named_args() { - let world = "world"; - - for lvl in log::Level::iter() { - log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori"; "hello {world}"); - - log!(lvl; cat_1 = "chashu", cat_2 = "nori"; "hello {world}"); - } - - all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori"; "hello {world}"); - all_log_macros!(cat_1 = "chashu", cat_2 = "nori"; "hello {world}"); -} - #[test] #[cfg(feature = "kv_unstable")] fn kv_expr_context() { @@ -207,3 +162,58 @@ fn kv_expr_context() { } }; } + +#[test] +#[cfg(feature = "kv_unstable")] +fn kv_shadow_target() { + all_log_macros!(target = "kv_target"; "hello {}", "world"); + all_log_macros!(target = "kv_target", cat_1 = "chashu", cat_2 = "nori"; "hello {}", "world"); +} + +#[rustversion::since(1.58)] +mod implicit_args { + use super::*; + + #[test] + fn implicit_named_args() { + let world = "world"; + + for lvl in log::Level::iter() { + log!(lvl, "hello {world}"); + log!(lvl, "hello {world}",); + + log!(target: "my_target", lvl, "hello {world}"); + log!(target: "my_target", lvl, "hello {world}",); + + log!(lvl; "hello {world}"); + log!(lvl; "hello {world}",); + + log!(target = "my_target", lvl; "hello {world}"); + log!(target = "my_target", lvl; "hello {world}",); + } + + all_log_macros!("hello {world}"); + all_log_macros!("hello {world}",); + + all_log_macros!(target = "my_target"; "hello {world}"); + all_log_macros!(target = "my_target"; "hello {world}",); + + all_log_macros!(target: "my_target", "hello {world}"); + all_log_macros!(target: "my_target", "hello {world}",); + } + + #[test] + #[cfg(feature = "kv_unstable")] + fn kv_implicit_named_args() { + let world = "world"; + + for lvl in log::Level::iter() { + log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori"; "hello {world}"); + + log!(lvl; cat_1 = "chashu", cat_2 = "nori"; "hello {world}"); + } + + all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori"; "hello {world}"); + all_log_macros!(cat_1 = "chashu", cat_2 = "nori"; "hello {world}"); + } +} From 13b9dbcef1a87c0b35b4434b703861914d80ae3f Mon Sep 17 00:00:00 2001 From: KodrAus Date: Sun, 12 Dec 2021 11:00:46 +1000 Subject: [PATCH 04/10] add a different prop type to macro tests --- tests/macros.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/macros.rs b/tests/macros.rs index dcc88f851..20e905e4a 100644 --- a/tests/macros.rs +++ b/tests/macros.rs @@ -118,39 +118,39 @@ fn expr() { #[cfg(feature = "kv_unstable")] fn kv_no_args() { for lvl in log::Level::iter() { - log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori"; "hello"); + log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); - log!(lvl; cat_1 = "chashu", cat_2 = "nori"; "hello"); + log!(lvl; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); } - all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori"; "hello"); - all_log_macros!(cat_1 = "chashu", cat_2 = "nori"; "hello"); + all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); + all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); } #[test] #[cfg(feature = "kv_unstable")] fn kv_anonymous_args() { for lvl in log::Level::iter() { - log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori"; "hello {}", "world"); + log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world"); - log!(lvl; cat_1 = "chashu", cat_2 = "nori"; "hello {}", "world"); + log!(lvl; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world"); } - all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori"; "hello {}", "world"); - all_log_macros!(cat_1 = "chashu", cat_2 = "nori"; "hello {}", "world"); + all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world"); + all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world"); } #[test] #[cfg(feature = "kv_unstable")] fn kv_named_args() { for lvl in log::Level::iter() { - log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori"; "hello {world}", world = "world"); + log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world"); - log!(lvl; cat_1 = "chashu", cat_2 = "nori"; "hello {world}", world = "world"); + log!(lvl; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world"); } - all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori"; "hello {world}", world = "world"); - all_log_macros!(cat_1 = "chashu", cat_2 = "nori"; "hello {world}", world = "world"); + all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world"); + all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world"); } #[test] @@ -167,7 +167,7 @@ fn kv_expr_context() { #[cfg(feature = "kv_unstable")] fn kv_shadow_target() { all_log_macros!(target = "kv_target"; "hello {}", "world"); - all_log_macros!(target = "kv_target", cat_1 = "chashu", cat_2 = "nori"; "hello {}", "world"); + all_log_macros!(target = "kv_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world"); } #[rustversion::since(1.58)] @@ -208,12 +208,12 @@ mod implicit_args { let world = "world"; for lvl in log::Level::iter() { - log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori"; "hello {world}"); + log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}"); - log!(lvl; cat_1 = "chashu", cat_2 = "nori"; "hello {world}"); + log!(lvl; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}"); } - all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori"; "hello {world}"); - all_log_macros!(cat_1 = "chashu", cat_2 = "nori"; "hello {world}"); + all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}"); + all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}"); } } From d6e44eb2539bd27127c7bb666d87b11906a788d0 Mon Sep 17 00:00:00 2001 From: KodrAus Date: Sun, 12 Dec 2021 11:02:50 +1000 Subject: [PATCH 05/10] add an expr test to macros --- tests/macros.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/macros.rs b/tests/macros.rs index 20e905e4a..56dbbf285 100644 --- a/tests/macros.rs +++ b/tests/macros.rs @@ -127,6 +127,19 @@ fn kv_no_args() { all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); } +#[test] +#[cfg(feature = "kv_unstable")] +fn kv_expr_args() { + for lvl in log::Level::iter() { + log!(target = "my_target", lvl; cat_math = { let mut x = 0; x += 1; x + 1 }; "hello"); + + log!(lvl; cat_math = { let mut x = 0; x += 1; x + 1 }; "hello"); + } + + all_log_macros!(target = "my_target"; cat_math = { let mut x = 0; x += 1; x + 1 }; "hello"); + all_log_macros!(cat_math = { let mut x = 0; x += 1; x + 1 }; "hello"); +} + #[test] #[cfg(feature = "kv_unstable")] fn kv_anonymous_args() { From 7ed6401b93dd151273859e57b15f96e716f31069 Mon Sep 17 00:00:00 2001 From: KodrAus Date: Sun, 12 Dec 2021 11:13:26 +1000 Subject: [PATCH 06/10] run fmt --- src/kv/value.rs | 20 +++++++++++++++----- tests/macros.rs | 14 +++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/kv/value.rs b/src/kv/value.rs index 727c249ea..c349b8c85 100644 --- a/src/kv/value.rs +++ b/src/kv/value.rs @@ -40,34 +40,44 @@ impl<'v> ToValue for Value<'v> { /// Get a value from a type implementing `std::fmt::Debug`. #[macro_export] macro_rules! as_debug { - ($capture:expr) => ($crate::kv::Value::from_debug(&$capture)); + ($capture:expr) => { + $crate::kv::Value::from_debug(&$capture) + }; } /// Get a value from a type implementing `std::fmt::Display`. #[macro_export] macro_rules! as_display { - ($capture:expr) => ($crate::kv::Value::from_display(&$capture)); + ($capture:expr) => { + $crate::kv::Value::from_display(&$capture) + }; } /// Get a value from an error. #[cfg(feature = "kv_unstable_std")] #[macro_export] macro_rules! as_error { - ($capture:expr) => ($crate::kv::Value::from_dyn_error(&$capture)); + ($capture:expr) => { + $crate::kv::Value::from_dyn_error(&$capture) + }; } #[cfg(feature = "kv_unstable_serde")] /// Get a value from a type implementing `serde::Serialize`. #[macro_export] macro_rules! as_serde { - ($capture:expr) => ($crate::kv::Value::from_serde(&$capture)); + ($capture:expr) => { + $crate::kv::Value::from_serde(&$capture) + }; } /// Get a value from a type implementing `sval::value::Value`. #[cfg(feature = "kv_unstable_sval")] #[macro_export] macro_rules! as_sval { - ($capture:expr) => ($crate::kv::Value::from_sval(&$capture)); + ($capture:expr) => { + $crate::kv::Value::from_sval(&$capture) + }; } /// A value in a structured key-value pair. diff --git a/tests/macros.rs b/tests/macros.rs index 56dbbf285..3b4dbd3a8 100644 --- a/tests/macros.rs +++ b/tests/macros.rs @@ -190,27 +190,27 @@ mod implicit_args { #[test] fn implicit_named_args() { let world = "world"; - + for lvl in log::Level::iter() { log!(lvl, "hello {world}"); log!(lvl, "hello {world}",); - + log!(target: "my_target", lvl, "hello {world}"); log!(target: "my_target", lvl, "hello {world}",); - + log!(lvl; "hello {world}"); log!(lvl; "hello {world}",); - + log!(target = "my_target", lvl; "hello {world}"); log!(target = "my_target", lvl; "hello {world}",); } - + all_log_macros!("hello {world}"); all_log_macros!("hello {world}",); - + all_log_macros!(target = "my_target"; "hello {world}"); all_log_macros!(target = "my_target"; "hello {world}",); - + all_log_macros!(target: "my_target", "hello {world}"); all_log_macros!(target: "my_target", "hello {world}",); } From 505c255f0a2e0eab946bb5740060d844bd7f2684 Mon Sep 17 00:00:00 2001 From: KodrAus Date: Sun, 12 Dec 2021 11:20:05 +1000 Subject: [PATCH 07/10] try putting attribute on fn --- tests/macros.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/tests/macros.rs b/tests/macros.rs index 3b4dbd3a8..11f88503e 100644 --- a/tests/macros.rs +++ b/tests/macros.rs @@ -183,12 +183,10 @@ fn kv_shadow_target() { all_log_macros!(target = "kv_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world"); } -#[rustversion::since(1.58)] -mod implicit_args { - use super::*; - - #[test] - fn implicit_named_args() { +#[test] +fn implicit_named_args() { + #[rustversion::since(1.58)] + fn _check() { let world = "world"; for lvl in log::Level::iter() { @@ -214,10 +212,13 @@ mod implicit_args { all_log_macros!(target: "my_target", "hello {world}"); all_log_macros!(target: "my_target", "hello {world}",); } +} - #[test] - #[cfg(feature = "kv_unstable")] - fn kv_implicit_named_args() { +#[test] +#[cfg(feature = "kv_unstable")] +fn kv_implicit_named_args() { + #[rustversion::since(1.58)] + fn _check() { let world = "world"; for lvl in log::Level::iter() { From 699afffdc0af9d4801940fa61035edf407ef82bd Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Thu, 23 Dec 2021 15:07:25 +1000 Subject: [PATCH 08/10] revert requirement for target = in kv macros --- src/macros.rs | 71 +++++++++++------------------------- tests/macros.rs | 97 ++++++++++++++++++------------------------------- 2 files changed, 57 insertions(+), 111 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 24331cc26..d1177c99a 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -29,8 +29,8 @@ /// ``` #[macro_export(local_inner_macros)] macro_rules! log { - // log!(target = "my_target", Level::Info; key1 = 42, key2 = true; "a {} event", "log"); - (target = $target:expr, $lvl:expr; $($key:ident = $value:expr),+; $($arg:tt)+) => ({ + // log!(target: "my_target", Level::Info; key1 = 42, key2 = true; "a {} event", "log"); + (target: $target:expr, $lvl:expr, $($key:ident = $value:expr),+; $($arg:tt)+) => ({ let lvl = $lvl; if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() { $crate::__private_api_log( @@ -42,8 +42,8 @@ macro_rules! log { } }); - // log!(target = "my_target", Level::Info; "a {} event", "log"); - (target = $target:expr, $lvl:expr; $($arg:tt)+) => ({ + // log!(target: "my_target", Level::Info; "a {} event", "log"); + (target: $target:expr, $lvl:expr, $($arg:tt)+) => ({ let lvl = $lvl; if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() { $crate::__private_api_log( @@ -55,17 +55,8 @@ macro_rules! log { } }); - // log!(target: "my_target", Level::Info, "a log event") - (target: $target:expr, $lvl:expr, $($arg:tt)+) => (log!(target = $target, $lvl; $($arg)+)); - - // log!(target = "my_target", Level::Info; "a log event") - (target = $target:expr, $lvl:expr; $($arg:tt)+) => (log!(target = $target, $lvl; $($arg)+)); - // log!(Level::Info, "a log event") - ($lvl:expr, $($arg:tt)+) => (log!(target = __log_module_path!(), $lvl; $($arg)+)); - - // log!(Level::Info; "a log event") - ($lvl:expr; $($arg:tt)+) => (log!(target = __log_module_path!(), $lvl; $($arg)+)) + ($lvl:expr, $($arg:tt)+) => (log!(target: __log_module_path!(), $lvl, $($arg)+)); } /// Logs a message at the error level. @@ -84,15 +75,12 @@ macro_rules! log { /// ``` #[macro_export(local_inner_macros)] macro_rules! error { - // error!(target = "my_target"; key1 = 42, key2 = true; "a {} event", "log") - // error!(target = "my_target"; "a {} event", "log") - (target = $target:expr; $($arg:tt)+) => (log!(target = $target, $crate::Level::Error; $($arg)+)); - + // error!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log") // error!(target: "my_target", "a {} event", "log") - (target: $target:expr, $($arg:tt)+) => (log!(target = $target, $crate::Level::Error; $($arg)+)); + (target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Error, $($arg)+)); // error!("a {} event", "log") - ($($arg:tt)+) => (log!($crate::Level::Error; $($arg)+)) + ($($arg:tt)+) => (log!($crate::Level::Error, $($arg)+)) } /// Logs a message at the warn level. @@ -111,15 +99,12 @@ macro_rules! error { /// ``` #[macro_export(local_inner_macros)] macro_rules! warn { - // warn!(target = "my_target"; key1 = 42, key2 = true; "a {} event", "log") - // warn!(target = "my_target"; "a {} event", "log") - (target = $target:expr; $($arg:tt)+) => (log!(target = $target, $crate::Level::Warn; $($arg)+)); - + // warn!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log") // warn!(target: "my_target", "a {} event", "log") - (target: $target:expr, $($arg:tt)+) => (log!(target = $target, $crate::Level::Warn; $($arg)+)); + (target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Warn, $($arg)+)); // warn!("a {} event", "log") - ($($arg:tt)+) => (log!($crate::Level::Warn; $($arg)+)) + ($($arg:tt)+) => (log!($crate::Level::Warn, $($arg)+)) } /// Logs a message at the info level. @@ -140,15 +125,12 @@ macro_rules! warn { /// ``` #[macro_export(local_inner_macros)] macro_rules! info { - // info!(target = "my_target"; key1 = 42, key2 = true; "a {} event", "log") - // info!(target = "my_target"; "a {} event", "log") - (target = $target:expr; $($arg:tt)+) => (log!(target = $target, $crate::Level::Info; $($arg)+)); - + // info!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log") // info!(target: "my_target", "a {} event", "log") - (target: $target:expr, $($arg:tt)+) => (log!(target = $target, $crate::Level::Info; $($arg)+)); + (target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Info, $($arg)+)); // info!("a {} event", "log") - ($($arg:tt)+) => (log!($crate::Level::Info; $($arg)+)) + ($($arg:tt)+) => (log!($crate::Level::Info, $($arg)+)) } /// Logs a message at the debug level. @@ -168,15 +150,12 @@ macro_rules! info { /// ``` #[macro_export(local_inner_macros)] macro_rules! debug { - // debug!(target = "my_target"; key1 = 42, key2 = true; "a {} event", "log") - // debug!(target = "my_target"; "a {} event", "log") - (target = $target:expr; $($arg:tt)+) => (log!(target = $target, $crate::Level::Debug; $($arg)+)); - + // debug!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log") // debug!(target: "my_target", "a {} event", "log") - (target: $target:expr, $($arg:tt)+) => (log!(target = $target, $crate::Level::Debug; $($arg)+)); + (target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Debug, $($arg)+)); // debug!("a {} event", "log") - ($($arg:tt)+) => (log!($crate::Level::Debug; $($arg)+)) + ($($arg:tt)+) => (log!($crate::Level::Debug, $($arg)+)) } /// Logs a message at the trace level. @@ -198,15 +177,12 @@ macro_rules! debug { /// ``` #[macro_export(local_inner_macros)] macro_rules! trace { - // trace!(target = "my_target"; key1 = 42, key2 = true; "a {} event", "log") - // trace!(target = "my_target"; "a {} event", "log") - (target = $target:expr; $($arg:tt)+) => (log!(target = $target, $crate::Level::Trace; $($arg)+)); - + // trace!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log") // trace!(target: "my_target", "a {} event", "log") - (target: $target:expr, $($arg:tt)+) => (log!(target = $target, $crate::Level::Trace; $($arg)+)); + (target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Trace, $($arg)+)); // trace!("a {} event", "log") - ($($arg:tt)+) => (log!($crate::Level::Trace; $($arg)+)) + ($($arg:tt)+) => (log!($crate::Level::Trace, $($arg)+)) } /// Determines if a message logged at the specified level in that module will @@ -237,17 +213,14 @@ macro_rules! trace { /// ``` #[macro_export(local_inner_macros)] macro_rules! log_enabled { - (target = $target:expr, $lvl:expr) => {{ + (target: $target:expr, $lvl:expr) => {{ let lvl = $lvl; lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() && $crate::__private_api_enabled(lvl, $target) }}; - (target: $target:expr, $lvl:expr) => { - log_enabled!(target = $target, $lvl) - }; ($lvl:expr) => { - log_enabled!(target = __log_module_path!(), $lvl) + log_enabled!(target: __log_module_path!(), $lvl) }; } diff --git a/tests/macros.rs b/tests/macros.rs index 11f88503e..5ae94cc75 100644 --- a/tests/macros.rs +++ b/tests/macros.rs @@ -21,19 +21,13 @@ fn no_args() { log!(target: "my_target", lvl, "hello"); log!(target: "my_target", lvl, "hello",); - log!(lvl; "hello"); - log!(lvl; "hello",); - - log!(target = "my_target", lvl; "hello"); - log!(target = "my_target", lvl; "hello",); + log!(lvl, "hello"); + log!(lvl, "hello",); } all_log_macros!("hello"); all_log_macros!("hello",); - all_log_macros!(target = "my_target"; "hello"); - all_log_macros!(target = "my_target"; "hello",); - all_log_macros!(target: "my_target", "hello"); all_log_macros!(target: "my_target", "hello",); } @@ -47,19 +41,13 @@ fn anonymous_args() { log!(target: "my_target", lvl, "hello {}", "world"); log!(target: "my_target", lvl, "hello {}", "world",); - log!(lvl; "hello {}", "world"); - log!(lvl; "hello {}", "world",); - - log!(target = "my_target", lvl; "hello {}", "world"); - log!(target = "my_target", lvl; "hello {}", "world",); + log!(lvl, "hello {}", "world"); + log!(lvl, "hello {}", "world",); } all_log_macros!("hello {}", "world"); all_log_macros!("hello {}", "world",); - all_log_macros!(target = "my_target"; "hello {}", "world"); - all_log_macros!(target = "my_target"; "hello {}", "world",); - all_log_macros!(target: "my_target", "hello {}", "world"); all_log_macros!(target: "my_target", "hello {}", "world",); } @@ -73,19 +61,13 @@ fn named_args() { log!(target: "my_target", lvl, "hello {world}", world = "world"); log!(target: "my_target", lvl, "hello {world}", world = "world",); - log!(lvl; "hello {world}", world = "world"); - log!(lvl; "hello {world}", world = "world",); - - log!(target = "my_target", lvl; "hello {world}", world = "world"); - log!(target = "my_target", lvl; "hello {world}", world = "world",); + log!(lvl, "hello {world}", world = "world"); + log!(lvl, "hello {world}", world = "world",); } all_log_macros!("hello {world}", world = "world"); all_log_macros!("hello {world}", world = "world",); - all_log_macros!(target = "my_target"; "hello {world}", world = "world"); - all_log_macros!(target = "my_target"; "hello {world}", world = "world",); - all_log_macros!(target: "my_target", "hello {world}", world = "world"); all_log_macros!(target: "my_target", "hello {world}", world = "world",); } @@ -98,19 +80,13 @@ fn enabled() { } else { false }; - - let _enabled = if log_enabled!(target = "my_target", lvl) { - true - } else { - false - }; } } #[test] fn expr() { for lvl in log::Level::iter() { - let _ = log!(lvl; "hello"); + let _ = log!(lvl, "hello"); } } @@ -118,12 +94,13 @@ fn expr() { #[cfg(feature = "kv_unstable")] fn kv_no_args() { for lvl in log::Level::iter() { - log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); + log!(target: "my_target", lvl, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); - log!(lvl; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); + log!(lvl, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); } - all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); + all_log_macros!(target: "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); + all_log_macros!(target = "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello"); } @@ -131,12 +108,14 @@ fn kv_no_args() { #[cfg(feature = "kv_unstable")] fn kv_expr_args() { for lvl in log::Level::iter() { - log!(target = "my_target", lvl; cat_math = { let mut x = 0; x += 1; x + 1 }; "hello"); + log!(target: "my_target", lvl, cat_math = { let mut x = 0; x += 1; x + 1 }; "hello"); - log!(lvl; cat_math = { let mut x = 0; x += 1; x + 1 }; "hello"); + log!(lvl, target = "my_target", cat_math = { let mut x = 0; x += 1; x + 1 }; "hello"); + log!(lvl, cat_math = { let mut x = 0; x += 1; x + 1 }; "hello"); } - all_log_macros!(target = "my_target"; cat_math = { let mut x = 0; x += 1; x + 1 }; "hello"); + all_log_macros!(target: "my_target", cat_math = { let mut x = 0; x += 1; x + 1 }; "hello"); + all_log_macros!(target = "my_target", cat_math = { let mut x = 0; x += 1; x + 1 }; "hello"); all_log_macros!(cat_math = { let mut x = 0; x += 1; x + 1 }; "hello"); } @@ -144,12 +123,14 @@ fn kv_expr_args() { #[cfg(feature = "kv_unstable")] fn kv_anonymous_args() { for lvl in log::Level::iter() { - log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world"); + log!(target: "my_target", lvl, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world"); + log!(lvl, target = "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world"); - log!(lvl; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world"); + log!(lvl, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world"); } - all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world"); + all_log_macros!(target: "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world"); + all_log_macros!(target = "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world"); all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world"); } @@ -157,12 +138,13 @@ fn kv_anonymous_args() { #[cfg(feature = "kv_unstable")] fn kv_named_args() { for lvl in log::Level::iter() { - log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world"); + log!(target: "my_target", lvl, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world"); + log!(lvl, target = "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world"); - log!(lvl; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world"); + log!(lvl, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world"); } - all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world"); + all_log_macros!(target: "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world"); all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world"); } @@ -171,18 +153,11 @@ fn kv_named_args() { fn kv_expr_context() { match "chashu" { cat_1 => { - info!(target = "target"; cat_1 = cat_1, cat_2 = "nori"; "hello {}", "cats") + info!(target: "target", cat_1 = cat_1, cat_2 = "nori"; "hello {}", "cats") } }; } -#[test] -#[cfg(feature = "kv_unstable")] -fn kv_shadow_target() { - all_log_macros!(target = "kv_target"; "hello {}", "world"); - all_log_macros!(target = "kv_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world"); -} - #[test] fn implicit_named_args() { #[rustversion::since(1.58)] @@ -196,21 +171,18 @@ fn implicit_named_args() { log!(target: "my_target", lvl, "hello {world}"); log!(target: "my_target", lvl, "hello {world}",); - log!(lvl; "hello {world}"); - log!(lvl; "hello {world}",); - - log!(target = "my_target", lvl; "hello {world}"); - log!(target = "my_target", lvl; "hello {world}",); + log!(lvl, "hello {world}"); + log!(lvl, "hello {world}",); } all_log_macros!("hello {world}"); all_log_macros!("hello {world}",); - all_log_macros!(target = "my_target"; "hello {world}"); - all_log_macros!(target = "my_target"; "hello {world}",); - all_log_macros!(target: "my_target", "hello {world}"); all_log_macros!(target: "my_target", "hello {world}",); + + all_log_macros!(target = "my_target"; "hello {world}"); + all_log_macros!(target = "my_target"; "hello {world}",); } } @@ -222,12 +194,13 @@ fn kv_implicit_named_args() { let world = "world"; for lvl in log::Level::iter() { - log!(target = "my_target", lvl; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}"); + log!(target: "my_target", lvl, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}"); - log!(lvl; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}"); + log!(lvl, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}"); } - all_log_macros!(target = "my_target"; cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}"); + all_log_macros!(target: "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}"); + all_log_macros!(target = "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}"); all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}"); } } From 26aef64c46e6873e8cac2cb29967e813a0618620 Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Thu, 23 Dec 2021 15:13:42 +1000 Subject: [PATCH 09/10] fix up kv examples --- README.md | 2 +- src/lib.rs | 4 ++-- tests/macros.rs | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c67207f1a..a26192778 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ If you enable the `kv_unstable` feature, you can associate structured data with use log::{info, trace, warn, as_serde, as_error}; pub fn shave_the_yak(yak: &mut Yak) { - trace!(target = "yak_events"; yak = as_serde!(yak); "Commencing yak shaving"); + trace!(target = "yak_events", yak = as_serde!(yak); "Commencing yak shaving"); loop { match find_a_razor() { diff --git a/src/lib.rs b/src/lib.rs index 4123a712c..781152a3a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,7 +55,7 @@ //! use log::{info, warn}; //! //! pub fn shave_the_yak(yak: &mut Yak) { -//! info!(target = "yak_events"; "Commencing yak shaving for {:?}", yak); +//! info!(target = "yak_events", "Commencing yak shaving for {:?}", yak); //! //! loop { //! match find_a_razor() { @@ -102,7 +102,7 @@ //! use log::{info, warn, as_serde, as_error}; //! //! pub fn shave_the_yak(yak: &mut Yak) { -//! info!(target = "yak_events"; yak = as_serde!(yak); "Commencing yak shaving"); +//! info!(target = "yak_events", yak = as_serde!(yak); "Commencing yak shaving"); //! //! loop { //! match find_a_razor() { diff --git a/tests/macros.rs b/tests/macros.rs index 5ae94cc75..4bb73da52 100644 --- a/tests/macros.rs +++ b/tests/macros.rs @@ -145,6 +145,7 @@ fn kv_named_args() { } all_log_macros!(target: "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world"); + all_log_macros!(target = "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world"); all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world"); } From 806eb7fbb48a7fc177c63d289c1fcb1545f8a73e Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Thu, 30 Dec 2021 12:12:22 +1000 Subject: [PATCH 10/10] fix up CI --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 781152a3a..373745579 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,7 +55,7 @@ //! use log::{info, warn}; //! //! pub fn shave_the_yak(yak: &mut Yak) { -//! info!(target = "yak_events", "Commencing yak shaving for {:?}", yak); +//! info!(target: "yak_events", "Commencing yak shaving for {:?}", yak); //! //! loop { //! match find_a_razor() { @@ -102,7 +102,7 @@ //! use log::{info, warn, as_serde, as_error}; //! //! pub fn shave_the_yak(yak: &mut Yak) { -//! info!(target = "yak_events", yak = as_serde!(yak); "Commencing yak shaving"); +//! info!(target: "yak_events", yak = as_serde!(yak); "Commencing yak shaving"); //! //! loop { //! match find_a_razor() {