Eliza Weisman b385da6b02
subscriber: better default filtering (#336)
## Feature Request

### Crates

- `tracing-subscriber`

### Motivation

Currently, the `tracing-subscriber` `FmtSubscriber::default`
implementation defaults to no filtering. This means that all
instrumentation, including extremely verbose `trace`-level diagnostics
from crates like `tokio` are enabled by default.

This is because the subscriber itself does not implement filtering, in
order to allow it to be composed with filters implemented by `Layer`s.
However, defaulting to no filtering at all is definitely a surprising
behavior. I didn't want to conditionally return a different type based
on whether or not filtering was enabled by the `filter` feature flag,
but this is probably not worth the confusion introduced by this
behavior. We should make this more intuitive.

## Solution

This branch changes `tracing-subscriber` to default to enabling
the `INFO` level and above. If the `filter` feature flag is enabled,
users may opt-in to `env_logger`-style filtering. Additionally, 
regardless of feature flags, a new `with_max_level` method is
added to the `FmtSubscriber` builder, which takes a `Level` or
`LevelFilter`. `LevelFilter` now implements `Layer` by enabling
any spans and events that are less than or equal to that `Level`.

Fixes: #331 
Fixes: #332

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2019-09-10 16:04:55 -07:00

48 lines
1.1 KiB
Rust

#![deny(rust_2018_idioms)]
/// This is a example showing how information is scoped.
///
/// You can run this example by running the following command in a terminal
///
/// ```
/// cargo +nightly run --example spawny_thing
/// ```
use tokio;
use futures::future::join_all;
use tracing::{debug, info};
use tracing_attributes::instrument;
#[instrument]
async fn parent_task(subtasks: usize) {
info!("spawning subtasks...");
let subtasks = (1..=subtasks)
.map(|number| {
debug!(message = "creating subtask;", number);
subtask(number)
})
.collect::<Vec<_>>();
let result = join_all(subtasks).await;
debug!("all subtasks completed");
let sum: usize = result.into_iter().sum();
info!(sum);
}
#[instrument]
async fn subtask(number: usize) -> usize {
info!("polling subtask...");
number
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let subscriber = tracing_subscriber::fmt::Subscriber::builder()
.with_max_level(tracing::Level::DEBUG)
.finish();
tracing::subscriber::set_global_default(subscriber)?;
parent_task(10).await;
Ok(())
}