pub struct Logger { /* private fields */ }Expand description
The Logger struct used to print logs.
§Example
let mut logger = Logger::default();
logger.debug("debug message");
logger.info("info message");
logger.warning("warning message");
logger.error("error message");
logger.fatal("fatal error message");Implementations§
Source§impl Logger
impl Logger
Sourcepub fn set_verbosity<I: Into<Verbosity>>(&mut self, verbosity: I)
pub fn set_verbosity<I: Into<Verbosity>>(&mut self, verbosity: I)
Sourcepub fn toggle_log_filtering<I: Into<bool>>(&mut self, enabled: I)
pub fn toggle_log_filtering<I: Into<bool>>(&mut self, enabled: I)
Toggles log filtering.
- true: logs will get filtered based on verbosity
- false: log filtering will be disabled globally
Sourcepub fn toggle_log_header_color<I: Into<bool>>(&mut self, enabled: I)
pub fn toggle_log_header_color<I: Into<bool>>(&mut self, enabled: I)
Toggles colored log headers.
true: Log headers will have colorsfalse: No colors :(
Sourcepub fn set_debug_color<I: Into<Color>>(&mut self, color: I)
pub fn set_debug_color<I: Into<Color>>(&mut self, color: I)
Sets debug log header color.
Sourcepub fn set_info_color<I: Into<Color>>(&mut self, color: I)
pub fn set_info_color<I: Into<Color>>(&mut self, color: I)
Sets info log header color.
Sourcepub fn set_warning_color<I: Into<Color>>(&mut self, color: I)
pub fn set_warning_color<I: Into<Color>>(&mut self, color: I)
Sets warning header color.
Sourcepub fn set_error_color<I: Into<Color>>(&mut self, color: I)
pub fn set_error_color<I: Into<Color>>(&mut self, color: I)
Sets error header color.
Sourcepub fn set_fatal_color<I: Into<Color>>(&mut self, color: I)
pub fn set_fatal_color<I: Into<Color>>(&mut self, color: I)
Sets fatal error header color.
Sourcepub fn set_debug_header(&mut self, header: &str)
pub fn set_debug_header(&mut self, header: &str)
Sets debug log header format.
Sourcepub fn set_info_header(&mut self, header: &str)
pub fn set_info_header(&mut self, header: &str)
Sets info log header format.
Sourcepub fn set_warning_header(&mut self, header: &str)
pub fn set_warning_header(&mut self, header: &str)
Sets warning header format.
Sourcepub fn set_error_header(&mut self, header: &str)
pub fn set_error_header(&mut self, header: &str)
Sets error header format.
Sourcepub fn set_fatal_header(&mut self, header: &str)
pub fn set_fatal_header(&mut self, header: &str)
Sets fatal error header format.
Sourcepub fn set_datetime_format(&mut self, format: &str)
pub fn set_datetime_format(&mut self, format: &str)
Sets datetime format.
Sourcepub fn set_log_format(&mut self, format: &str) -> Result<(), String>
pub fn set_log_format(&mut self, format: &str) -> Result<(), String>
Sets the log format.
There are three placeholders in a log format string (you can place multiple placeholders of the same type in a format string):
%c: Ascending log count starting at 1.%d: The timestamp.%h: The header indicating the log type (e.g., debug, error, etc.)%m: The log message (this placeholder is mandatory, you will get an error if you don’t include this in your log format).
§Example
l.set_log_format("<l> <h>%h</h> <m>%m</m> </l>");
l.error("lorem ipsum");Returns an error when the %m placeholder is missing.
Sourcepub fn set_log_file_path(&mut self, path: &str) -> Result<(), Error>
pub fn set_log_file_path(&mut self, path: &str) -> Result<(), Error>
Sets log file path.
Returns an error if the path is inaccessible.
§Example
// Set the log file path first:
logger.set_log_file_path(path);
// Then enable file logging:
logger.toggle_file_logging(true);Sourcepub fn toggle_file_logging<I: Into<bool>>(
&mut self,
enabled: I,
) -> Result<(), Error>
pub fn toggle_file_logging<I: Into<bool>>( &mut self, enabled: I, ) -> Result<(), Error>
Toggles file logging.
Before enabling file logging, ensure that the log file path is set. This is because this method checks if the log file is writable. If the log file path is not set, or the file is not writable, enabling file logging will result in an error.
§Example
// Set the log file path first:
logger.set_log_file_path(path);
// Then enable file logging:
logger.toggle_file_logging(true);Sourcepub fn set_max_log_buffer_size<I: Into<u32>>(&mut self, size: I)
pub fn set_max_log_buffer_size<I: Into<u32>>(&mut self, size: I)
Sets the maximum size of the log buffer.
This method sets the maximum allowed size for the log buffer. When the
buffer exceeds this size, it will be automatically flushed to the log
file. If the buffer size is set to 0, automatic flushing is disabled,
and the buffer can only be flushed manually.
If a log file lock is active, the log buffer will not be flushed automatically, regardless of the size limit.
§Example
let mut logger = Logger::default();
logger.set_log_file_path(path);
logger.toggle_file_logging(true);
// This will make `Logger` to flush the log buffer every 16 logs:
logger.set_max_log_buffer_size(16 as u32);
let mut i = 0;
loop {
logger.info("Yay!");
i += 1;
if i >= 16 {
break;
}
}Sourcepub fn toggle_log_file_lock<I: Into<bool>>(&mut self, enabled: I)
pub fn toggle_log_file_lock<I: Into<bool>>(&mut self, enabled: I)
Log file lock can be used to prevent race conditions when there is one thread reading from the log file and another thread writing to the log file.
§WARNING: leaving this option on for a long period of time will cause
high memory usage!
true: When log file lock is enabled, logger won’t flush into the log file. Instead, it will wait until the lock is disabled. You will not loose any logs, they will be stored in the log buffer even when it exceeds its size limit.false: Logger will write to the log file normally.
Sourcepub fn set_on_drop_file_policy<I: Into<OnDropPolicy>>(&mut self, policy: I)
pub fn set_on_drop_file_policy<I: Into<OnDropPolicy>>(&mut self, policy: I)
Sets Logger’s on drop log file policy.
§Example
logger.set_on_drop_file_policy(OnDropPolicy::IgnoreLogFileLock);Sourcepub fn toggle_console_output<I: Into<bool>>(&mut self, enabled: I)
pub fn toggle_console_output<I: Into<bool>>(&mut self, enabled: I)
Toggles printing logs to stdout.
true: Logs will be printed in your terminal’sstdout.false: No log output in your terminal.
Sourcepub fn toggle_custom_log_buffer<I: Into<bool>>(&mut self, enabled: I)
pub fn toggle_custom_log_buffer<I: Into<bool>>(&mut self, enabled: I)
Toggles the usage of a custom log buffer.
true: Logs will be stored in a buffer insideLoggerand can be cloned using theclone_log_buffer()method. Be aware that this will lead to high memory usage if turned on for a log period of time.false: Logs will not be stored in a log buffer.
Sourcepub fn clear_log_buffer(&mut self)
pub fn clear_log_buffer(&mut self)
Clears the custom log buffer.
Source§impl Logger
impl Logger
Sourcepub fn from_template(path: &str) -> Logger
pub fn from_template(path: &str) -> Logger
Creates a Logger instance from a template file.
Automatically expands env variables.
Logger templates documentation.
§Example
let mut logger = Logger::from_template(path);Sourcepub fn save_template(&self, path: &str)
pub fn save_template(&self, path: &str)
Saves a Logger to template file.
Automatically expands env variables.
Logger templates documentation
§Example
let mut logger = Logger::default();
logger.save_template(path);Source§impl Logger
impl Logger
Sourcepub fn format_log(&self, log: &LogStruct) -> String
pub fn format_log(&self, log: &LogStruct) -> String
Returns a log entry from a LogStruct based on current Logger
configuration.
§Example:
let log_string = logger.format_log(&LogStruct::error("eror"));Sourcepub fn flush(&mut self) -> Result<(), String>
pub fn flush(&mut self) -> Result<(), String>
Flushes log buffer (if file logging is enabled and log file lock disabled, it writes the log buffer to a file).
Returns an error when there is an issue writing to a file or log file lock is enabled.
Sourcepub fn debug_no_filtering(&mut self, message: &str)
pub fn debug_no_filtering(&mut self, message: &str)
Prints a debug message to stdout, bypasses filtering.
Sourcepub fn info_no_filtering(&mut self, message: &str)
pub fn info_no_filtering(&mut self, message: &str)
Prints an informational message to stdout, bypasses filtering.
Sourcepub fn warning_no_filtering(&mut self, message: &str)
pub fn warning_no_filtering(&mut self, message: &str)
Prints a warning to stdout, bypasses filtering.
Sourcepub fn log_buffer<'a>(&'a self) -> &'a Vec<LogStruct>
pub fn log_buffer<'a>(&'a self) -> &'a Vec<LogStruct>
Returns a reference to the custom log buffer.