How to filter-out tracing logs?

2.6k Views Asked by At

I'm trying to use the tracing framework when making a web server. When I use tracing and send requests, there will be log output from the hyper crate. How do I filter out these logs and keep the logs of my application itself?

I have this code:

let filter: EnvFilter = "hyper=debug".parse().expect("filter should parse");
let logger = Registry::default()
    .with(EnvFilter::from_default_env().add_directive(log_env.into()).with_filter(filter))
    .with(fmt::Layer::default().event_format(format));

tracing::subscriber::set_global_default(logger).unwrap();

but it does not work for me.

1

There are 1 best solutions below

1
exlinx On

in your Cargo.toml

tracing-subscriber = {version="0.3", features=["env-filter"]}

in your code:

tracing_subscriber::fmt::fmt()
        .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
        .init();

then:

RUST_LOG=my_crate=DEBUG cargo run