pub fn init_log_crate_proxy() -> Result<(), SetLoggerError>Available on crate feature
log only.Expand description
Initializes the log crate proxy.
This function calls log::set_logger to set up a LogCrateProxy and
all logs from log crate will be forwarded to spdlog-rs’s logger.
Users should call this function only once, and then configure the proxy by
calling log_crate_proxy().
Note that the log crate uses a different log level filter and by default
it rejects all log messages. To log messages via the log crate, you have
to call log::set_max_level manually before logging. For more
information, please read the upstream documentation of
log::set_max_level.
Examples found in repository?
examples/06-log-crate.rs (line 2)
1fn main() -> Result<(), Box<dyn std::error::Error>> {
2 spdlog::init_log_crate_proxy()
3 .expect("users should only call `init_log_crate_proxy` function once");
4
5 // Setup filter as needed.
6 let filter = env_filter::Builder::new().try_parse("RUST_LOG")?.build();
7 spdlog::log_crate_proxy().set_filter(Some(filter));
8
9 log::set_max_level(log::LevelFilter::Trace);
10 log::trace!("this log will be processed by the global default logger in spdlog-rs");
11
12 let custom_logger = spdlog::default_logger().fork_with_name(Some("another_logger"))?;
13 spdlog::log_crate_proxy().set_logger(Some(custom_logger));
14 log::info!("this log will be processed by custom_logger in spdlog-rs");
15
16 spdlog::log_crate_proxy().set_logger(None);
17 log::trace!("this log will be processed by the global default logger in spdlog-rs");
18
19 Ok(())
20}