pub struct Logger { /* private fields */ }
Expand description
The Logger struct.
Logger::new() returns a LoggerBuilder. Call the build method on the LoggerBuilder instance to get a Logger instance.
§Example
let mut logger = Logger::new()
.level(LoggingLevel::Error)
.filename(PathBuf::from("Logs.json"))
.build()
.unwrap();
logger.info("informative msg".to_string());
logger.critical("CRITICAL ERROR".to_string());
“informative msg” will not be printed as it is info, but level is set to error. “CRITICAL ERROR” will be printed as critical has a higher precendece than error.
If logging to a file is not needed
§Example
let mut logger = Logger::new()
.level(LoggingLevel::Trace)
.build()
.unwrap();
logger.info("Informative message");
The build method will not return an error here as no file is being opened. The fields for file, json, filename, and json_object will be set None.
Json is parsed differently compared to normal parsing.
An example of normal parsing. Date is in YYYY-MM-DD format. All timestamps are in local timezone based on system time.
§Example
2024-03-06 12:04:01 CRITICAL: msg
2024-03-06 12:04:01 ERROR: msg
An example of json parsing
{
"logs": [
{
"date": "2024-03-06",
"time": "12:01:53",
"message": "msg",
"type": "CRITICAL"
},
{
"date": "2024-03-06",
"time": "12:01:53",
"message": "msg",
"type": "ERROR"
}
]
}
Implementations§
source§impl Logger
impl Logger
sourcepub fn new() -> LoggerBuilder
pub fn new() -> LoggerBuilder
Constructor function. Returns a LoggerBuilder instance.
sourcepub fn get_filename(&self) -> Option<&PathBuf>
pub fn get_filename(&self) -> Option<&PathBuf>
Returns a reference to the filename.
It will return None if log_to_file is false
sourcepub fn set_level(&mut self, level: LoggingLevel)
pub fn set_level(&mut self, level: LoggingLevel)
Set the logging level