Crates.io | rs-logger |
lib.rs | rs-logger |
version | 0.3.2 |
source | src |
created_at | 2022-07-22 15:46:24.428551 |
updated_at | 2024-03-10 20:09:27.152075 |
description | A simple to use rust logging library. |
homepage | |
repository | https://github.com/Anmsh27/rs-logger |
max_upload_size | |
id | 630870 |
size | 18,181 |
A simple to use logging library for the Rust programming language.
Add rs-logger = "0.3.2" in your Cargo.toml file under dependencies.
[dependencies]
rs-logger = "0.3.2"
Or run cargo add rs-logger
in the terminal.
There are five different levels of logging.
Each level has its own precedence. Trace allows for everything to be printed, Warning allows only warning() and higher precedence messages to be printed, etc. The highest is critical(). It is important to note that all messages will be stored in the log file regardless of logging level, if logging to file is true.
The constructor new() for Logger returns an instance of the LoggerBuilder struct, where level() and filename() can be called to set the level and filename. If filename() is not called, then logging to a file will be turned off.
Call the build method on the LoggerBuilder instance to get an instance of Logger.
The build() method will not return an error if logging to file is off.
Example of normal parsing. Date is in YYYY-MM-DD format
2024-03-06 12:04:01 CRITICAL: msg
2024-03-06 12:04:01 ERROR: msg
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"
}
]
}
All timestamps and dates are in the local timezone based on system time.
use rs_logger::*;
fn main() {
let mut logger = Logger::new()
.level(LoggingLevel::Info)
.filename(PathBuf::from("logs.json"))
.build()
.unwrap();
logger.info("Informative message at date {%D} and time {%T}".to_string());
logger.critical("CRITICAL ERROR".to_string());
}
Trace => Everything
Info => Everything except debug
Warning => Warning, error, and critical,
Error => Error and critical
Critical => Only critical.