6

As a developer I would like to adjust the log level on the fly. For example, I don't want to log debug! events when everything is going fine, but when something happens, I would like to adjust the log level without restarting the application to change the log level. I check the documentation and can't find an example there, so I want to know if it is possible to do that.

// how can I change the max_level of subscriber after it was initialised?
let subscriber = tracing_subscriber::fmt().with_max_level(Level::INFO).finish();
tracing::subscriber::set_global_default(subscriber);

debug!("some log message");
kmdreko
  • 42,554
  • 6
  • 57
  • 106
Sean
  • 2,990
  • 1
  • 21
  • 31
  • 1
    I think you can use https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/fn.filter_fn.html with a global variable you can have a dynamic global level filter – Stargateur May 08 '22 at 09:19
  • @Stargateur this api looks promising. thanks for help – Sean May 08 '22 at 15:00

2 Answers2

7

You can use reload handler exactly for this. Save remote handler in some global state and call it to change log level. See example under "Reloading a Filtered layer"

ndrkrtshv
  • 106
  • 1
  • 5
-4

Use log4rs instead. It reads configs from a file which you can change on the fly. I use it in one of my projects, it works like a charm.

youwenti
  • 133
  • 9