Struct spdlog::Logger

source ·
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.

Sets the logger 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 print to stderr and then ignored.

Examples
use spdlog::prelude::*;

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

Fork and configure a separate new logger.

This function creates a new logger object that inherits logger properties from Arc<Self>. Then this function calls the given modifier function which configures the properties on the new logger object. The created new logger object will be a separate object from Arc<Self>. (No ownership sharing)

Examples
let old: Arc<Logger> = /* ... */
// Fork from an existing logger and add a new sink.
let new: Arc<Logger> = old.fork_with(|new: &mut Logger| {
    new.sinks_mut().push(new_sink);
    Ok(())
})?;

info!(logger: new, "this record will be written to `new_sink`");
info!(logger: old, "this record will not be written to `new_sink`");

Fork a separate new logger with a new name.

This function creates a new logger object that inherits logger properties from Arc<Self> and rename the new logger object to the given name. The created new logger object will be a separate object from Arc<Self>. (No ownership sharing)

This is a shorthand wrapper for Logger::fork_with.

Examples
let old: Arc<Logger> = Arc::new(Logger::builder().name("dog").build()?);
let new: Arc<Logger> = old.fork_with_name(Some("cat"))?;

assert_eq!(old.name(), Some("dog"));
assert_eq!(new.name(), Some("cat"));

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 alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
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.