How can I change the log level on the fly with tracing?

5.2k views Asked by At

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");
2

There are 2 answers

1
ndrkrtshv On BEST ANSWER

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"

0
youwenti On

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.