Skip to content

Commit f0f7494

Browse files
authored
Merge pull request #613 from rust-lang/feat/kv-cleanup
Get structured logging API ready for stabilization
2 parents 7cb6a01 + 2b220bf commit f0f7494

File tree

14 files changed

+1406
-494
lines changed

14 files changed

+1406
-494
lines changed

.github/workflows/main.yml

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ jobs:
4444
- run: cargo test --verbose --all-features
4545
- run: cargo test --verbose --features serde
4646
- run: cargo test --verbose --features std
47-
- run: cargo test --verbose --features kv_unstable
48-
- run: cargo test --verbose --features kv_unstable_sval
49-
- run: cargo test --verbose --features kv_unstable_serde
50-
- run: cargo test --verbose --features "kv_unstable kv_unstable_std kv_unstable_sval kv_unstable_serde"
47+
- run: cargo test --verbose --features kv
48+
- run: cargo test --verbose --features kv_sval
49+
- run: cargo test --verbose --features kv_serde
50+
- run: cargo test --verbose --features "kv kv_std kv_sval kv_serde"
5151
- run: cargo run --verbose --manifest-path test_max_level_features/Cargo.toml
5252
- run: cargo run --verbose --manifest-path test_max_level_features/Cargo.toml --release
5353

@@ -103,12 +103,12 @@ jobs:
103103
run: |
104104
rustup update nightly --no-self-update
105105
rustup default nightly
106-
- run: cargo build --verbose -Z avoid-dev-deps --features kv_unstable
107-
- run: cargo build --verbose -Z avoid-dev-deps --features "kv_unstable std"
108-
- run: cargo build --verbose -Z avoid-dev-deps --features "kv_unstable kv_unstable_sval"
109-
- run: cargo build --verbose -Z avoid-dev-deps --features "kv_unstable kv_unstable_serde"
110-
- run: cargo build --verbose -Z avoid-dev-deps --features "kv_unstable kv_unstable_std"
111-
- run: cargo build --verbose -Z avoid-dev-deps --features "kv_unstable kv_unstable_sval kv_unstable_serde"
106+
- run: cargo build --verbose -Z avoid-dev-deps --features kv
107+
- run: cargo build --verbose -Z avoid-dev-deps --features "kv std"
108+
- run: cargo build --verbose -Z avoid-dev-deps --features "kv kv_sval"
109+
- run: cargo build --verbose -Z avoid-dev-deps --features "kv kv_serde"
110+
- run: cargo build --verbose -Z avoid-dev-deps --features "kv kv_std"
111+
- run: cargo build --verbose -Z avoid-dev-deps --features "kv kv_sval kv_serde"
112112

113113
minimalv:
114114
name: Minimal versions
@@ -119,12 +119,12 @@ jobs:
119119
run: |
120120
rustup update nightly --no-self-update
121121
rustup default nightly
122-
- run: cargo build --verbose -Z minimal-versions --features kv_unstable
123-
- run: cargo build --verbose -Z minimal-versions --features "kv_unstable std"
124-
- run: cargo build --verbose -Z minimal-versions --features "kv_unstable kv_unstable_sval"
125-
- run: cargo build --verbose -Z minimal-versions --features "kv_unstable kv_unstable_serde"
126-
- run: cargo build --verbose -Z minimal-versions --features "kv_unstable kv_unstable_std"
127-
- run: cargo build --verbose -Z minimal-versions --features "kv_unstable kv_unstable_sval kv_unstable_serde"
122+
- run: cargo build --verbose -Z minimal-versions --features kv
123+
- run: cargo build --verbose -Z minimal-versions --features "kv std"
124+
- run: cargo build --verbose -Z minimal-versions --features "kv kv_sval"
125+
- run: cargo build --verbose -Z minimal-versions --features "kv kv_serde"
126+
- run: cargo build --verbose -Z minimal-versions --features "kv kv_std"
127+
- run: cargo build --verbose -Z minimal-versions --features "kv kv_sval kv_serde"
128128

129129
msrv:
130130
name: MSRV
@@ -135,7 +135,9 @@ jobs:
135135
run: |
136136
rustup update 1.60.0 --no-self-update
137137
rustup default 1.60.0
138-
- run: cargo test --verbose --manifest-path tests/Cargo.toml
138+
- run: |
139+
cargo test --verbose --manifest-path tests/Cargo.toml
140+
cargo test --verbose --manifest-path tests/Cargo.toml --features kv
139141
140142
embedded:
141143
name: Embedded

Cargo.toml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ rust-version = "1.60.0"
1717
edition = "2021"
1818

1919
[package.metadata.docs.rs]
20-
features = ["std", "serde", "kv_unstable_std", "kv_unstable_sval", "kv_unstable_serde"]
20+
features = ["std", "serde", "kv_std", "kv_sval", "kv_serde"]
2121

2222
[[test]]
2323
name = "filters"
@@ -46,25 +46,31 @@ release_max_level_trace = []
4646

4747
std = []
4848

49-
# requires the latest stable
50-
# this will have a tighter MSRV before stabilization
51-
kv_unstable = ["value-bag"]
52-
kv_unstable_sval = ["kv_unstable", "value-bag/sval", "sval", "sval_ref"]
53-
kv_unstable_std = ["std", "kv_unstable", "value-bag/error"]
54-
kv_unstable_serde = ["kv_unstable_std", "value-bag/serde", "serde"]
49+
kv = []
50+
kv_sval = ["kv", "value-bag/sval", "sval", "sval_ref"]
51+
kv_std = ["std", "kv", "value-bag/error"]
52+
kv_serde = ["kv_std", "value-bag/serde", "serde"]
53+
54+
# Deprecated: use `kv_*` instead
55+
# These `*_unstable` features will be removed in a future release
56+
kv_unstable = ["kv", "value-bag"]
57+
kv_unstable_sval = ["kv_sval", "kv_unstable"]
58+
kv_unstable_std = ["kv_std", "kv_unstable"]
59+
kv_unstable_serde = ["kv_serde", "kv_unstable_std"]
5560

5661
[dependencies]
5762
serde = { version = "1.0", optional = true, default-features = false }
5863
sval = { version = "2.1", optional = true, default-features = false }
5964
sval_ref = { version = "2.1", optional = true, default-features = false }
60-
value-bag = { version = "1.4", optional = true, default-features = false }
65+
value-bag = { version = "1.7", optional = true, default-features = false, features = ["inline-i128"] }
6166

6267
[dev-dependencies]
6368
serde = { version = "1.0", features = ["derive"] }
69+
serde_json = "1.0"
6470
serde_test = "1.0"
6571
sval = { version = "2.1" }
6672
sval_derive = { version = "2.1" }
67-
value-bag = { version = "1.4", features = ["test"] }
73+
value-bag = { version = "1.7", features = ["test"] }
6874

6975
# NOTE: log doesn't actually depent on this crate. However our dependencies,
7076
# serde and sval, dependent on version 1.0 of the crate, which has problem fixed

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,28 @@ The executable itself may use the `log` crate to log as well.
100100

101101
## Structured logging
102102

103-
If you enable the `kv_unstable` feature, you can associate structured data with your log records:
103+
If you enable the `kv` feature, you can associate structured data with your log records:
104104

105105
```rust
106-
use log::{info, trace, warn, as_serde, as_error};
106+
use log::{info, trace, warn};
107107

108108
pub fn shave_the_yak(yak: &mut Yak) {
109-
trace!(target = "yak_events", yak = as_serde!(yak); "Commencing yak shaving");
109+
// `yak:serde` will capture `yak` using its `serde::Serialize` impl
110+
//
111+
// You could also use `:?` for `Debug`, or `:%` for `Display`. For a
112+
// full list, see the `log` crate documentation
113+
trace!(target = "yak_events", yak:serde; "Commencing yak shaving");
110114

111115
loop {
112116
match find_a_razor() {
113117
Ok(razor) => {
114-
info!(razor = razor; "Razor located");
118+
info!(razor; "Razor located");
115119
yak.shave(razor);
116120
break;
117121
}
118-
Err(err) => {
119-
warn!(err = as_error!(err); "Unable to locate a razor, retrying");
122+
Err(e) => {
123+
// `e:err` will capture `e` using its `std::error::Error` impl
124+
warn!(e:err; "Unable to locate a razor, retrying");
120125
}
121126
}
122127
}

benches/value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![cfg(feature = "kv_unstable")]
1+
#![cfg(feature = "kv")]
22
#![feature(test)]
33

44
use log::kv::Value;

src/__private_api.rs

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,28 @@ use crate::{Level, Metadata, Record};
55
use std::fmt::Arguments;
66
pub use std::{file, format_args, line, module_path, stringify};
77

8-
#[cfg(feature = "kv_unstable")]
9-
pub type Value<'a> = dyn crate::kv::value::ToValue + 'a;
10-
11-
#[cfg(not(feature = "kv_unstable"))]
12-
pub type Value<'a> = str;
8+
#[cfg(not(feature = "kv"))]
9+
pub type Value<'a> = &'a str;
1310

1411
mod sealed {
1512
/// Types for the `kv` argument.
1613
pub trait KVs<'a> {
17-
fn into_kvs(self) -> Option<&'a [(&'a str, &'a super::Value<'a>)]>;
14+
fn into_kvs(self) -> Option<&'a [(&'a str, super::Value<'a>)]>;
1815
}
1916
}
2017

2118
// Types for the `kv` argument.
2219

23-
impl<'a> KVs<'a> for &'a [(&'a str, &'a Value<'a>)] {
20+
impl<'a> KVs<'a> for &'a [(&'a str, Value<'a>)] {
2421
#[inline]
25-
fn into_kvs(self) -> Option<&'a [(&'a str, &'a Value<'a>)]> {
22+
fn into_kvs(self) -> Option<&'a [(&'a str, Value<'a>)]> {
2623
Some(self)
2724
}
2825
}
2926

3027
impl<'a> KVs<'a> for () {
3128
#[inline]
32-
fn into_kvs(self) -> Option<&'a [(&'a str, &'a Value<'a>)]> {
29+
fn into_kvs(self) -> Option<&'a [(&'a str, Value<'a>)]> {
3330
None
3431
}
3532
}
@@ -41,13 +38,11 @@ fn log_impl(
4138
level: Level,
4239
&(target, module_path, file): &(&str, &'static str, &'static str),
4340
line: u32,
44-
kvs: Option<&[(&str, &Value)]>,
41+
kvs: Option<&[(&str, Value)]>,
4542
) {
46-
#[cfg(not(feature = "kv_unstable"))]
43+
#[cfg(not(feature = "kv"))]
4744
if kvs.is_some() {
48-
panic!(
49-
"key-value support is experimental and must be enabled using the `kv_unstable` feature"
50-
)
45+
panic!("key-value support is experimental and must be enabled using the `kv` feature")
5146
}
5247

5348
let mut builder = Record::builder();
@@ -60,7 +55,7 @@ fn log_impl(
6055
.file_static(Some(file))
6156
.line(Some(line));
6257

63-
#[cfg(feature = "kv_unstable")]
58+
#[cfg(feature = "kv")]
6459
builder.key_values(&kvs);
6560

6661
crate::logger().log(&builder.build());
@@ -87,3 +82,44 @@ pub fn log<'a, K>(
8782
pub fn enabled(level: Level, target: &str) -> bool {
8883
crate::logger().enabled(&Metadata::builder().level(level).target(target).build())
8984
}
85+
86+
#[cfg(feature = "kv")]
87+
mod kv_support {
88+
use crate::kv;
89+
90+
pub type Value<'a> = kv::Value<'a>;
91+
92+
// NOTE: Many functions here accept a double reference &&V
93+
// This is so V itself can be ?Sized, while still letting us
94+
// erase it to some dyn Trait (because &T is sized)
95+
96+
pub fn capture_to_value<'a, V: kv::ToValue + ?Sized>(v: &'a &'a V) -> Value<'a> {
97+
v.to_value()
98+
}
99+
100+
pub fn capture_debug<'a, V: core::fmt::Debug + ?Sized>(v: &'a &'a V) -> Value<'a> {
101+
Value::from_debug(v)
102+
}
103+
104+
pub fn capture_display<'a, V: core::fmt::Display + ?Sized>(v: &'a &'a V) -> Value<'a> {
105+
Value::from_display(v)
106+
}
107+
108+
#[cfg(feature = "kv_std")]
109+
pub fn capture_error<'a>(v: &'a (dyn std::error::Error + 'static)) -> Value<'a> {
110+
Value::from_dyn_error(v)
111+
}
112+
113+
#[cfg(feature = "kv_sval")]
114+
pub fn capture_sval<'a, V: sval::Value + ?Sized>(v: &'a &'a V) -> Value<'a> {
115+
Value::from_sval(v)
116+
}
117+
118+
#[cfg(feature = "kv_serde")]
119+
pub fn capture_serde<'a, V: serde::Serialize + ?Sized>(v: &'a &'a V) -> Value<'a> {
120+
Value::from_serde(v)
121+
}
122+
}
123+
124+
#[cfg(feature = "kv")]
125+
pub use self::kv_support::*;

src/kv/error.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ enum Inner {
1111
#[cfg(feature = "std")]
1212
Boxed(std_support::BoxedError),
1313
Msg(&'static str),
14-
Value(value_bag::Error),
14+
#[cfg(feature = "value-bag")]
15+
Value(crate::kv::value::inner::Error),
1516
Fmt,
1617
}
1718

@@ -23,21 +24,23 @@ impl Error {
2324
}
2425
}
2526

26-
// Not public so we don't leak the `value_bag` API
27-
pub(super) fn from_value(err: value_bag::Error) -> Self {
27+
// Not public so we don't leak the `crate::kv::value::inner` API
28+
#[cfg(feature = "value-bag")]
29+
pub(super) fn from_value(err: crate::kv::value::inner::Error) -> Self {
2830
Error {
2931
inner: Inner::Value(err),
3032
}
3133
}
3234

33-
// Not public so we don't leak the `value_bag` API
34-
pub(super) fn into_value(self) -> value_bag::Error {
35+
// Not public so we don't leak the `crate::kv::value::inner` API
36+
#[cfg(feature = "value-bag")]
37+
pub(super) fn into_value(self) -> crate::kv::value::inner::Error {
3538
match self.inner {
3639
Inner::Value(err) => err,
37-
#[cfg(feature = "kv_unstable_std")]
38-
_ => value_bag::Error::boxed(self),
39-
#[cfg(not(feature = "kv_unstable_std"))]
40-
_ => value_bag::Error::msg("error inspecting a value"),
40+
#[cfg(feature = "kv_std")]
41+
_ => crate::kv::value::inner::Error::boxed(self),
42+
#[cfg(not(feature = "kv_std"))]
43+
_ => crate::kv::value::inner::Error::msg("error inspecting a value"),
4144
}
4245
}
4346
}
@@ -48,6 +51,7 @@ impl fmt::Display for Error {
4851
match &self.inner {
4952
#[cfg(feature = "std")]
5053
Boxed(err) => err.fmt(f),
54+
#[cfg(feature = "value-bag")]
5155
Value(err) => err.fmt(f),
5256
Msg(msg) => msg.fmt(f),
5357
Fmt => fmt::Error.fmt(f),

src/kv/key.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl ToKey for str {
3030
}
3131
}
3232

33-
/// A key in a structured key-value pair.
33+
/// A key in a key-value.
3434
// These impls must only be based on the as_str() representation of the key
3535
// If a new field (such as an optional index) is added to the key they must not affect comparison
3636
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -93,7 +93,7 @@ mod std_support {
9393
}
9494
}
9595

96-
#[cfg(feature = "kv_unstable_sval")]
96+
#[cfg(feature = "kv_sval")]
9797
mod sval_support {
9898
use super::*;
9999

@@ -116,7 +116,7 @@ mod sval_support {
116116
}
117117
}
118118

119-
#[cfg(feature = "kv_unstable_serde")]
119+
#[cfg(feature = "kv_serde")]
120120
mod serde_support {
121121
use super::*;
122122

0 commit comments

Comments
 (0)