forked from tokio-rs/tracing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
subscriber, serde: add
valuable
support to the JSON formatter (toki…
…o-rs#1862) This branch introduces support for `valuable` in `tracing-subscriber`'s JSON formatter, and in `tracing-serde`, using the `valuable-serde` bridge. This allows the `fmt::Json` subscriber to actually record `valuable` values as structured JSON. Here's an example, where a field is first recorded using `fmt::Debug`, and then again using `field::valuable`: ``` :; RUSTFLAGS="--cfg tracing_unstable" cargo run --example valuable_json | jq Finished dev [unoptimized + debuginfo] target(s) in 0.09s Running `target/debug/examples/valuable_json` { "timestamp": "2022-01-25T21:36:30.729658Z", "level": "INFO", "fields": { "valuable": false, "user": "User { name: \"Arwen Undomiel\", age: 3000, address: Address { country: \"Middle Earth\", city: \"Rivendell\", street: \"leafy lane\" } }" }, "target": "valuable_json" } { "timestamp": "2022-01-25T21:36:30.729720Z", "level": "INFO", "fields": { "valuable": true, "user": { "name": "Arwen Undomiel", "age": 3000, "address": { "country": "Middle Earth", "city": "Rivendell", "street": "leafy lane" } } }, "target": "valuable_json" } ``` As a side note, this branch also nicely validates that recording `Valuable` values from a subscriber is actually possible, which the previous valuable PR didn't have an example of. Signed-off-by: Eliza Weisman <eliza@buoyant.io>
- Loading branch information
Showing
5 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#![allow(dead_code)] | ||
//! This example shows how a field value may be recorded using the `valuable` | ||
//! crate (https://crates.io/crates/valuable). | ||
//! | ||
//! `valuable` provides a lightweight but flexible way to record structured data, allowing | ||
//! visitors to extract individual fields or elements of structs, maps, arrays, and other | ||
//! nested structures. | ||
//! | ||
//! `tracing`'s support for `valuable` is currently feature flagged. Additionally, `valuable` | ||
//! support is considered an *unstable feature*: in order to use `valuable` with `tracing`, | ||
//! the project must be built with `RUSTFLAGS="--cfg tracing_unstable`. | ||
//! | ||
//! Therefore, when `valuable` support is not enabled, this example falls back to using | ||
//! `fmt::Debug` to record fields that implement `valuable::Valuable`. | ||
#[cfg(tracing_unstable)] | ||
use tracing::field::valuable; | ||
use valuable::Valuable; | ||
|
||
#[derive(Clone, Debug, Valuable)] | ||
struct User { | ||
name: String, | ||
age: u32, | ||
address: Address, | ||
} | ||
|
||
#[derive(Clone, Debug, Valuable)] | ||
struct Address { | ||
country: String, | ||
city: String, | ||
street: String, | ||
} | ||
|
||
fn main() { | ||
tracing_subscriber::fmt() | ||
.with_max_level(tracing::Level::TRACE) | ||
.json() | ||
.init(); | ||
|
||
let user = User { | ||
name: "Arwen Undomiel".to_string(), | ||
age: 3000, | ||
address: Address { | ||
country: "Middle Earth".to_string(), | ||
city: "Rivendell".to_string(), | ||
street: "leafy lane".to_string(), | ||
}, | ||
}; | ||
|
||
// for comparison, record `user` without using its `Valuable` | ||
// implementation: | ||
tracing::info!(valuable = false, user = ?user); | ||
|
||
// If the `valuable` feature is enabled, record `user` using its' | ||
// `valuable::Valuable` implementation: | ||
#[cfg(tracing_unstable)] | ||
tracing::info!(valuable = true, user = valuable(&user)); | ||
|
||
#[cfg(not(tracing_unstable))] | ||
tracing::warn!( | ||
"note: this example was run without `valuable` support enabled!\n\ | ||
rerun with `RUSTFLAGS=\"--cfg tracing_unstable\" to enable `valuable`", | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters