Skip to content

Commit

Permalink
add examples of overriding formatters
Browse files Browse the repository at this point in the history
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
hawkw committed Apr 3, 2020
1 parent ed2d128 commit a4507eb
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ This directory contains a collection of examples that demonstrate the use of the
which provides a subscriber implementation that logs traces to the console.
+ `fmt-stderr`: Demonstrates overriding the output stream used by the `fmt`
subscriber.
+ `fmt-custom-field`: Demonstrates overriding how the `fmt` subscriber formats
fields on spans and events.
+ `fmt-custom-event`: Demonstrates overriding how the `fmt` subscriber formats
events.
+ `subscriber-filter`: Demonstrates the `tracing-subscriber::filter` module,
which provides a layer which adds configurable filtering to a subscriber
implementation.
Expand Down
29 changes: 29 additions & 0 deletions examples/examples/fmt-custom-event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#![deny(rust_2018_idioms)]
use tracing_subscriber;
#[path = "fmt/yak_shave.rs"]
mod yak_shave;

fn main() {
use tracing_subscriber::fmt;

// Configure a custom event formatter
let format = fmt::format()
.compact() // use an abbreviated format for logging spans
.with_level(false) // don't include levels in formatted output
.with_target(false); // don't include targets

// Create a `fmt` subscriber that uses our custom event format, and set it
// as the default.
fmt().event_format(format).init();

// Shave some yaks!
let number_of_yaks = 3;
// this creates a new event, outside of any spans.
tracing::info!(number_of_yaks, "preparing to shave yaks");

let number_shaved = yak_shave::shave_all(number_of_yaks);
tracing::info!(
all_yaks_shaved = number_shaved == number_of_yaks,
"yak shaving completed."
);
}
37 changes: 37 additions & 0 deletions examples/examples/fmt-custom-field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! This example demonstrates overriding the way `tracing-subscriber`'s
//! `FmtSubscriber` formats fields on spans and events, using a closure.
#![deny(rust_2018_idioms)]
use tracing::Level;
use tracing_subscriber;

#[path = "fmt/yak_shave.rs"]
mod yak_shave;

fn main() {
use tracing_subscriber::{fmt::format, prelude::*};

// Format fields using the provided closure.
let format = format::debug_fn(|writer, _field, value| {
// We'll format _just_ the value of each field, ignoring the field name.
write!(writer, "{:?}", value)
})
// Separate each field with a comma.
// This method is provided by an extension trait in the
// `tracing-subscriber` prelude.
.delimited(", ");

// Create a `fmt` subscriber that uses our custom event format, and set it
// as the default.
tracing_subscriber::fmt().fmt_fields(format).init();

// Shave some yaks!
let number_of_yaks = 3;
// this creates a new event, outside of any spans.
tracing::info!(number_of_yaks, "preparing to shave yaks");

let number_shaved = yak_shave::shave_all(number_of_yaks);
tracing::info!(
all_yaks_shaved = number_shaved == number_of_yaks,
"yak shaving completed."
);
}

0 comments on commit a4507eb

Please sign in to comment.