9 releases
0.3.2 | Mar 10, 2024 |
---|---|
0.3.1 | Mar 10, 2024 |
0.2.0 | Mar 2, 2024 |
0.1.8 |
|
0.1.7 | Jul 26, 2022 |
#211 in Debugging
433 downloads per month
18KB
272 lines
Rs-Logger
A simple to use logging library for the Rust programming language.
Installation
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.
Code Examples
There are five different levels of logging.
- Trace
- Info
- Warning
- Error
- Critical
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());
}
Levels
-
Trace => Everything
-
Info => Everything except debug
-
Warning => Warning, error, and critical,
-
Error => Error and critical
-
Critical => Only critical.
Dependencies
~2–12MB
~76K SLoC