LogCrateProxy

Struct LogCrateProxy 

Source
pub struct LogCrateProxy { /* private fields */ }
Available on crate feature 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

Source

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.

Source

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?
examples/06-log-crate.rs (line 13)
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}
Source

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?
examples/06-log-crate.rs (line 7)
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}

Trait Implementations§

Source§

impl Default for LogCrateProxy

Source§

fn default() -> LogCrateProxy

Returns the “default value” for a type. Read more
Source§

impl Log for LogCrateProxy

Source§

fn enabled(&self, metadata: &Metadata<'_>) -> bool

Determines if a log message with the specified metadata would be logged. Read more
Source§

fn log(&self, record: &Record<'_>)

Logs the Record. Read more
Source§

fn flush(&self)

Flushes any buffered records. Read more

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.