pub struct LogCrateProxy { /* private fields */ }log only.Expand description
Proxy layer for compatible log crate.
Call init_log_crate_proxy to initialize the proxy, and then configure
the proxy via log_crate_proxy.
After the proxy is initialized, it will forward all log messages from log
crate to the global default logger or the logger set by
LogCrateProxy::set_logger.
To set filters or read from RUST_LOG variable, call
LogCrateProxy::set_filter after initialization.
Note that the log crate uses a different log level filter and by default
it rejects all log messages. To make LogCrateProxy able to receive log
messages from log crate, you may need to call
re_export::log::set_max_level with re_export::log::LevelFilter.
§Examples
use spdlog::re_export::log;
spdlog::init_log_crate_proxy()?;
// Enable all log messages from `log` crate.
log::set_max_level(log::LevelFilter::Trace);For more and detailed examples, see ./examples directory.
Implementations§
Source§impl LogCrateProxy
impl LogCrateProxy
Sourcepub fn swap_logger(&self, logger: Option<Arc<Logger>>) -> Option<Arc<Logger>>
pub fn swap_logger(&self, logger: Option<Arc<Logger>>) -> Option<Arc<Logger>>
Sets a logger as the new receiver, and returens the old one.
If the argument logger is None, the global default logger will be
used.
Sourcepub fn set_logger(&self, logger: Option<Arc<Logger>>)
pub fn set_logger(&self, logger: Option<Arc<Logger>>)
Sets a logger as the new receiver.
If the argument logger is None, the global default logger will be
used.
Examples found in repository?
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}Sourcepub fn set_filter(&self, filter: Option<Filter>)
pub fn set_filter(&self, filter: Option<Filter>)
Sets a filter for records from log crate.
This is useful if users want to support RUST_LOG environment variable.
Examples found in repository?
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}