pub struct Logger { /* private fields */ }
Expand description
A simple, flexible logger that supports multiple targets and custom formats
Implementations§
Source§impl Logger
impl Logger
Sourcepub fn with_level(level: LogLevel) -> Self
pub fn with_level(level: LogLevel) -> Self
Create a logger with a specific log level
Sourcepub fn from_env() -> Self
pub fn from_env() -> Self
Create a logger configured from environment variables
This method checks for log level configuration in the following order:
- RUST_LOG environment variable (Rust convention)
- LOG_LEVEL environment variable (fallback)
If neither is found or both are invalid, defaults to Info level.
Sourcepub fn time_format(self, format: &str) -> Self
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”
Sourcepub fn no_time_prefix(self) -> Self
pub fn no_time_prefix(self) -> Self
Disable time prefix
This sets the time format to empty string, effectively removing timestamps.
Sourcepub fn format_for_level(self, level: LogLevel, format: &str) -> Self
pub fn format_for_level(self, level: LogLevel, format: &str) -> Self
Set custom format string for a specific log level
Sourcepub fn stdout(self) -> Self
pub fn stdout(self) -> Self
Set a target to write logs to (stdout) - replaces all existing targets
Sourcepub fn stderr(self) -> Self
pub fn stderr(self) -> Self
Set a target to write logs to (stderr) - replaces all existing targets
Sourcepub fn file(self, path: &str) -> Result<Self>
pub fn file(self, path: &str) -> Result<Self>
Add a file as a target - replaces all existing targets
Sourcepub fn custom<W>(self, target: W) -> Self
pub fn custom<W>(self, target: W) -> Self
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(())
}
Sourcepub fn add_stdout(self) -> Self
pub fn add_stdout(self) -> Self
Add a stdout target to existing targets
Sourcepub fn add_stderr(self) -> Self
pub fn add_stderr(self) -> Self
Add a stderr target to existing targets
Sourcepub fn add_target<W>(self, target: W) -> Self
pub fn add_target<W>(self, target: W) -> Self
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(())
}
Sourcepub fn log(&self, message: &str) -> Result<()>
pub fn log(&self, message: &str) -> Result<()>
Log a message (always outputs, no level filtering)
Sourcepub fn log_lazy<F>(&self, message_fn: F) -> Result<()>
pub fn log_lazy<F>(&self, message_fn: F) -> Result<()>
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(())
}
pub fn warning(&self, message: &str) -> Result<()>
pub fn info(&self, message: &str) -> Result<()>
pub fn debug(&self, message: &str) -> Result<()>
pub fn trace(&self, message: &str) -> Result<()>
Sourcepub fn error_lazy<F>(&self, message_fn: F) -> Result<()>
pub fn error_lazy<F>(&self, message_fn: F) -> Result<()>
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.
pub fn warning_lazy<F>(&self, message_fn: F) -> Result<()>
pub fn info_lazy<F>(&self, message_fn: F) -> Result<()>
pub fn debug_lazy<F>(&self, message_fn: F) -> Result<()>
pub fn trace_lazy<F>(&self, message_fn: F) -> Result<()>
Sourcepub fn set_time_format(&mut self, format: &str)
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”
Sourcepub fn disable_time_prefix(&mut self)
pub fn disable_time_prefix(&mut self)
Disable time prefix
This sets the time format to empty string, effectively removing timestamps.
Sourcepub fn set_format_for_level(&mut self, level: LogLevel, format: &str)
pub fn set_format_for_level(&mut self, level: LogLevel, format: &str)
Set custom format string for a specific log level
Sourcepub fn get_level(&self) -> LogLevel
pub fn get_level(&self) -> LogLevel
Get the current log level (alias for level for compatibility)
Sourcepub fn clear_targets(self) -> Self
pub fn clear_targets(self) -> Self
Clear all targets