Logger

Struct Logger 

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

A simple, flexible logger that supports multiple targets and custom formats

Implementations§

Source§

impl Logger

Source

pub fn new() -> Self

Create a new logger with default settings

Source

pub fn with_level(level: LogLevel) -> Self

Create a logger with a specific log level

Source

pub fn from_env() -> Self

Create a logger configured from environment variables

This method checks for log level configuration in the following order:

  1. RUST_LOG environment variable (Rust convention)
  2. LOG_LEVEL environment variable (fallback)

If neither is found or both are invalid, defaults to Info level.

Source

pub fn time_format(self, format: &str) -> Self

Set the time format using chrono format string

Common formats:

  • %Y-%m-%d %H:%M:%S - “2025-09-14 16:57:00” (default)
  • %H:%M:%S - “16:57:00”
  • %Y-%m-%d %H:%M:%S%.3f - “2025-09-14 16:57:00.123”
  • %Y-%m-%d - “2025-09-14”
Source

pub fn no_time_prefix(self) -> Self

Disable time prefix

This sets the time format to empty string, effectively removing timestamps.

Source

pub fn format_for_level(self, level: LogLevel, format: &str) -> Self

Set custom format string for a specific log level

Source

pub fn stdout(self) -> Self

Set a target to write logs to (stdout) - replaces all existing targets

Source

pub fn stderr(self) -> Self

Set a target to write logs to (stderr) - replaces all existing targets

Source

pub fn file(self, path: &str) -> Result<Self>

Add a file as a target - replaces all existing targets

Source

pub fn custom<W>(self, target: W) -> Self
where W: Write + Send + Sync + 'static,

Set a custom Write target - replaces all existing targets

This allows you to set any type that implements Write + Send + Sync as the only logging target. Useful for custom writers, network streams, or any other Write implementor.

§Examples
use nonblocking_logger::Logger;
use std::io::Write;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut buffer = Vec::new();
    let logger = Logger::new().custom(buffer);
     
    logger.info("This will be written to the custom target only")?;
    Ok(())
}
Source

pub fn add_stdout(self) -> Self

Add a stdout target to existing targets

Source

pub fn add_stderr(self) -> Self

Add a stderr target to existing targets

Source

pub fn add_file(self, path: &str) -> Result<Self>

Add a file target to existing targets

Source

pub fn add_target<W>(self, target: W) -> Self
where W: Write + Send + Sync + 'static,

Add a custom Write target to existing targets

This allows you to add any type that implements Write + Send + Sync as a logging target. Useful for custom writers, network streams, or any other Write implementor.

§Examples
use nonblocking_logger::Logger;
use std::io::Write;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut buffer = Vec::new();
    let logger = Logger::new().add_target(buffer);
     
    logger.info("This will be written to the custom target")?;
    Ok(())
}
Source

pub fn log(&self, message: &str) -> Result<()>

Log a message (always outputs, no level filtering)

Source

pub fn log_lazy<F>(&self, message_fn: F) -> Result<()>
where F: FnOnce() -> String,

Log a message with lazy evaluation (always outputs, no level filtering)

This is more efficient when the message requires expensive computation, as the closure will only be executed if the log level allows the message to be output.

§Examples
use nonblocking_logger::Logger;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let logger = Logger::new();
     
    // This expensive computation will always run and output
    logger.log_lazy(|| {
        format!("Expensive computation result: {}", "some_expensive_result")
    })?;
     
    Ok(())
}
Source

pub fn error(&self, message: &str) -> Result<()>

Convenience methods for each log level

Source

pub fn warning(&self, message: &str) -> Result<()>

Source

pub fn info(&self, message: &str) -> Result<()>

Source

pub fn debug(&self, message: &str) -> Result<()>

Source

pub fn trace(&self, message: &str) -> Result<()>

Source

pub fn error_lazy<F>(&self, message_fn: F) -> Result<()>
where F: FnOnce() -> String,

Convenience methods for each log level with lazy evaluation

These methods only execute the closure if the log level is sufficient, making them more efficient for expensive message computations.

Source

pub fn warning_lazy<F>(&self, message_fn: F) -> Result<()>
where F: FnOnce() -> String,

Source

pub fn info_lazy<F>(&self, message_fn: F) -> Result<()>
where F: FnOnce() -> String,

Source

pub fn debug_lazy<F>(&self, message_fn: F) -> Result<()>
where F: FnOnce() -> String,

Source

pub fn trace_lazy<F>(&self, message_fn: F) -> Result<()>
where F: FnOnce() -> String,

Source

pub fn set_level(&mut self, level: LogLevel)

Set the log level

Source

pub fn set_time_format(&mut self, format: &str)

Set the time format using chrono format string

Common formats:

  • %Y-%m-%d %H:%M:%S - “2025-09-14 16:57:00” (default)
  • %H:%M:%S - “16:57:00”
  • %Y-%m-%d %H:%M:%S%.3f - “2025-09-14 16:57:00.123”
  • %Y-%m-%d - “2025-09-14”
Source

pub fn disable_time_prefix(&mut self)

Disable time prefix

This sets the time format to empty string, effectively removing timestamps.

Source

pub fn set_format_for_level(&mut self, level: LogLevel, format: &str)

Set custom format string for a specific log level

Source

pub fn level(&self) -> LogLevel

Get the current log level

Source

pub fn get_level(&self) -> LogLevel

Get the current log level (alias for level for compatibility)

Source

pub fn clear_targets(self) -> Self

Clear all targets

Trait Implementations§

Source§

impl Default for Logger

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl Freeze for Logger

§

impl RefUnwindSafe for Logger

§

impl Send for Logger

§

impl Sync for Logger

§

impl Unpin for Logger

§

impl UnwindSafe for Logger

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.