MoSal

joined 2 years ago
[โ€“] MoSal@lemmyrs.org 5 points 2 years ago

It's weird how much difference a year makes in the Rust world. I had some initial pushback in this PR from 11 months ago, which only expanded the scope of recommendation for tracking Cargo.lock a little.

[โ€“] MoSal@lemmyrs.org 1 points 2 years ago

I think tracing-appender is not directly relevant here.

Never touched tracing layers before. But I think this may help you:

EnvFilter implements both the Layer and Filter traits, so it may be used for both global filtering and per-layer filtering, respectively. See the documentation on filtering with Layers for details.

https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html

So adding a filter with <custom-target>=<log-level> to your custom layer should work.

Example:

use tracing_subscriber::{fmt, EnvFilter, prelude::*};

fn main() {
    let custom_layer = fmt::layer()
        .with_writer(|| std::io::stderr())
        // EnvFilter as a Filter
        .with_filter(EnvFilter::try_new("custom_target=info").unwrap());

    tracing_subscriber::registry()
        // EnvFilter as a Layer
        .with(EnvFilter::try_new("info").unwrap())
        .with(fmt::layer()) // default
        .with(custom_layer) // custom
        .init();

    tracing::info!("default target");
    tracing::info!(target:"custom_target", "custom target");
}

Here the default layer is using the default writer to stdout, while the custom one is writing to stderr.