Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to modify EnvFilter instead of replace #3025

Open
wuanzhuan opened this issue Jul 6, 2024 · 3 comments
Open

How to modify EnvFilter instead of replace #3025

wuanzhuan opened this issue Jul 6, 2024 · 3 comments

Comments

@wuanzhuan
Copy link

in tracing_subscriber 0.3.18

I use a Envfilter in global subscriber. It is composed of one LevelFilter and Targets. When i modify the level filter, i need replace the entire EnvFilter. How do I only modify the LevelFilter in EnvFilter.

@kaffarell
Copy link
Contributor

Could you post a snippet? Also checkout https://docs.rs/tracing-subscriber/latest/tracing_subscriber/reload/index.html#examples. This should be possible if I understand your issue correctly.

@wuanzhuan
Copy link
Author

wuanzhuan commented Aug 8, 2024

@kaffarell I use the envfilter. Because of it can easy to "and" tow filter. I use one global level filter and some target filter in a env filter.
And i just want to modify the global level filter or one target filter. how to do it?

    // trace init:
    let subscriber = tracing_fmt::fmt()
        .with_timer(tracing_fmt::time::LocalTime::rfc_3339())
        .with_file(true)
        .with_line_number(true)
        .with_writer(non_blocking)
        .with_ansi(false)
        .with_env_filter(format!("info,sys_monitor::event_trace::stack_walk=debug")) // env filter
        .with_filter_reloading();
    let reload_handle = subscriber.reload_handle();
    subscriber.init();

//==================
   
   // How to modify env filter like this:
   reload_handle.modify(|filter| *filter = filter::LevelFilter::DEBUG);

@kaffarell
Copy link
Contributor

AFAIK editing the EnvFilter directly is not possible, but you can create a layer with multiple filters! For example something like this should work:

    let filter = filter::LevelFilter::WARN;
    let (filter, reload_handle) = reload::Layer::new(filter);

    let layer = tracing_subscriber::fmt::layer()
        .with_timer(tracing_subscriber::fmt::time::LocalTime::rfc_3339())
        .with_file(true)
        .with_line_number(true)
        .with_ansi(false)
        .with_filter(EnvFilter::new("trace,sys_monitor::event_trace::stack_walk=debug"))
        .with_filter(filter);

    tracing_subscriber::registry().with(layer).init();

    let _ = reload_handle.modify(|filter| *filter = filter::LevelFilter::DEBUG);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants