pub struct Logger {
pub formatter: Mutex<LogFormatter>,
pub output: LogOutput,
/* private fields */
}Expand description
Logger capable of filtering logs, formatting them and distributing them
to various streams.
The Logger struct is modular and itself only filters the logs, relying
on LogFormatter and LogOutput for log formatting and outputting.
Additionally, Logger includes a template system with built-in methods and
constructors for easy JSON serialization and deserialization.
§Examples
Using global logging:
use prettylogger::{debug, info, warn, err, fatal};
let mut message = "debug";
debug!("Hello, this is a {message} log!");
message = "info";
info!("Hello, this is an {message} log!");
message = "warning";
warn!("Hello, this is a {message}!");
message = "error";
err!("Hello, this is an {message}!");
message = "fatal error";
fatal!("Hello, this is a {message}!")Creating a Logger struct and printing out some logs:
// Create a `Logger` instance with default configuration
let mut logger = Logger::default();
// Print log messages
logger.debug("debug message");
logger.info("info message");
logger.warning("warning message");
logger.error("error message");
logger.fatal("fatal error message");Configuring the formatter of a Logger:
// Create a `Logger` instance with default configuration
let mut logger = Logger::default();
// Set a simple log format
logger.formatter.lock().unwrap().set_log_format("[ %d ] %m");
// Change debug log header color
logger.formatter.lock().unwrap().set_debug_color(Color::Red);
// Set a fatal log header
logger.formatter.lock().unwrap().set_fatal_header("--FATAL--");
// Configure datetime format
logger.formatter.lock().unwrap().set_datetime_format("%H:%M");Enabling log buffering:
// Create a `Logger` instance with default configuration
let mut logger = Logger::default();
// Enable log buffering
logger.output.buffer_output.lock().unwrap().enable();
// Write to the log buffer 128 times
for i in 0..128 {
logger.error(&format!("Error number {}", i));
}
// Get a reference to the log buffer
let buffer = logger.output.buffer_output.lock().unwrap().get_log_buffer();Enabling file logging:
// Create a `Logger` instance with default configuration
let mut logger = Logger::default();
// Lock the file output and obtain a reference to it
let mut file_output = logger.output.file_output.lock().unwrap();
// Required by `FileStream` for parsing logs
let mut formatter = LogFormatter::default();
// Set the log file path **first**
file_output.set_log_file_path(&path)
.expect("Failed to set the log file path!");
// Enable the output
file_output.enable().
expect("Failed to enable the output!");
// Write to the log file buffer
file_output.out(&LogStruct::debug("Hello from file!"),
&mut formatter).expect("Failed to write to the buffer!");
// Flush the logs from the buffer to the log file
file_output.flush();Fields§
§formatter: Mutex<LogFormatter>§output: LogOutputImplementations§
Source§impl Logger
impl Logger
Sourcepub fn from_template_str(template: &str) -> Result<Logger, Error>
pub fn from_template_str(template: &str) -> Result<Logger, Error>
Creates a Logger instance from a JSON template as string.
§Examples
Deserializing Logger from a JSON string:
let pretty_json = serde_json::to_string_pretty(&Logger::default())
.expect("Failed to serialize logger!");
let mut logger = Logger::from_template_str(&pretty_json)
.expect("Failed to deserialize logger!");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 enable_log_filtering(&mut self)
pub fn enable_log_filtering(&mut self)
Enables log filtering.
Sourcepub fn disable_log_filtering(&mut self)
pub fn disable_log_filtering(&mut self)
Disables log filtering.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Logger
impl<'de> Deserialize<'de> for Logger
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more