pub struct Logger { /* private fields */ }
Expand description

A logger structure.

A logger contains a combination of sinks, and sinks implement writing log messages to actual targets.

Users usually log messages through log macros.

Examples

use std::time::Duration;
use spdlog::prelude::*;

let default_logger: Arc<Logger> = spdlog::default_logger();
default_logger.set_level_filter(LevelFilter::All);
default_logger.set_flush_period(Some(Duration::from_secs(10)));
info!("logging with default logger");

custom_logger.set_level_filter(LevelFilter::All);
custom_logger.set_flush_period(Some(Duration::from_secs(10)));
info!(logger: custom_logger, "logging with custom logger");

For more examples, see ./examples directory.

Implementations

Constructs a LoggerBuilder.

Gets the logger name.

Returns None if the logger does not have a name.

Determines if a log message with the specified level would be logged.

This allows callers to avoid expensive computation of log message arguments if the message would be discarded anyway.

Examples
use spdlog::prelude::*;

let logger: Arc<Logger> = spdlog::default_logger();

logger.set_level_filter(LevelFilter::MoreSevere(Level::Info));
assert_eq!(logger.should_log(Level::Debug), false);
assert_eq!(logger.should_log(Level::Info), false);
assert_eq!(logger.should_log(Level::Warn), true);
assert_eq!(logger.should_log(Level::Error), true);

logger.set_level_filter(LevelFilter::All);
assert_eq!(logger.should_log(Level::Debug), true);
assert_eq!(logger.should_log(Level::Info), true);
assert_eq!(logger.should_log(Level::Warn), true);
assert_eq!(logger.should_log(Level::Error), true);

Logs a record.

Users usually do not use this function directly, use log macros instead.

Flushes any buffered records.

Users can call this function to flush manually or use auto-flush policies. See also Logger::flush_level_filter and Logger::set_flush_period.

Note that it is expensive, calling it frequently will affect performance.

Gets the flush level filter.

Sets a flush level filter.

When logging a new record, flush the buffer if this filter condition is true.

This auto-flush policy can work with Logger::set_flush_period together.

Examples
use spdlog::prelude::*;

logger.set_flush_level_filter(LevelFilter::Off);
trace!(logger: logger, "hello");
trace!(logger: logger, "world");
// Until here the buffer may not have been flushed (depending on sinks implementation)

logger.set_flush_level_filter(LevelFilter::All);
trace!(logger: logger, "hello"); // Logs and flushes the buffer once
trace!(logger: logger, "world"); // Logs and flushes the buffer once

Gets the log filter level.

Sets the log filter level.

Examples

See Logger::should_log.

Sets periodic flush.

This function receives a &Arc<Self>. Calling it will spawn a new thread.

This auto-flush policy can work with Logger::set_flush_level_filter together.

Panics
  • Panics if interval is zero.

  • Panics if this function is called with Some value and then clones the Logger instead of the Arc<Logger>.

Examples
use std::time::Duration;

// From now on, auto-flush the `logger` buffer every 10 seconds.
logger.set_flush_period(Some(Duration::from_secs(10)));

// Remove periodic auto-flush.
logger.set_flush_period(None);

Gets a reference to sinks in the logger.

Gets a mutable reference to sinks in the logger.

Sets a error handler.

If an error occurs while logging or flushing, this handler will be called. If no handler is set, the error will be output to the terminal and then ignored.

Examples
use spdlog::prelude::*;

spdlog::default_logger().set_error_handler(Some(|err: spdlog::Error| {
    panic!("spdlog-rs error: {}", err)
}));

Trait Implementations

Clones the Logger.

Panics

Panics if Logger::set_flush_period is called with Some value and then clones the Logger instead of the Arc<Logger>.

Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.