Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get structured logging API ready for stabilization #613

Merged
merged 27 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
dbc0a3f
start filling in docs and remove capture/downcast value support
KodrAus Jan 26, 2024
fdc1c6e
rename value::Visit to value::Visitor
KodrAus Jan 28, 2024
7f93aca
start factoring out value-bag
KodrAus Jan 29, 2024
e3a5b98
stub out the internal value APIs
KodrAus Jan 29, 2024
9fae53d
fill in the test token API
KodrAus Jan 29, 2024
05119e1
fill in some more doc examples
KodrAus Jan 29, 2024
0096377
some more kv docs
KodrAus Jan 29, 2024
54c34f7
start filling in no-dependency value
KodrAus Jan 30, 2024
05d7bed
fill in visitor for inline value
KodrAus Jan 31, 2024
e711b62
fix up some warnings
KodrAus Jan 31, 2024
67885e0
add some docs on how Value is implemented
KodrAus Jan 31, 2024
0374a25
work on macro support for capturing
KodrAus Feb 13, 2024
1d258b6
Merge branch 'feat/kv-cleanup' of https://github.com/rust-lang/log in…
KodrAus Feb 13, 2024
6b483c6
make capturing docs easier to read
KodrAus Feb 13, 2024
d8dc6a8
rename source::Visitor to VisitSource and value::Visitor to VisitValue
KodrAus Feb 13, 2024
f6b89c0
update and clarify docs, make internal kv modules private
KodrAus Feb 16, 2024
6d9e98a
run fmt
KodrAus Feb 16, 2024
44b8e99
fix up some warnings
KodrAus Feb 16, 2024
a6c4095
add a few more notes to the source
KodrAus Feb 16, 2024
52460f9
stabilize the kv features
KodrAus Feb 16, 2024
90a347b
restore removed APIs as deprecated
KodrAus Feb 18, 2024
ad91711
support field shorthand in macros
KodrAus Feb 18, 2024
31bb4b0
move error macros together
KodrAus Feb 18, 2024
73e9539
fix up capturing of :err
KodrAus Feb 18, 2024
cf85c38
add needed subfeatures to kv_unstable
KodrAus Feb 19, 2024
646e9ab
use original Visitor name for VisitValue
KodrAus Feb 19, 2024
2b220bf
clean up structured logging example
KodrAus Feb 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
KodrAus marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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).
//!
Expand Down
2 changes: 1 addition & 1 deletion src/kv/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
//! }
//! }
//! }
Expand Down
17 changes: 13 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
#[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::<&_>(
$crate::__private_api::format_args!($($arg)+),
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)*))),+]
);
}
});
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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"
);
}
Expand Down