9 releases

0.3.2 Mar 10, 2024
0.3.1 Mar 10, 2024
0.2.0 Mar 2, 2024
0.1.8 Mar 1, 2024
0.1.7 Jul 26, 2022

#172 in Debugging

Download history 206/week @ 2024-02-25 294/week @ 2024-03-03 250/week @ 2024-03-10 110/week @ 2024-03-17 3/week @ 2024-03-24 16/week @ 2024-03-31

148 downloads per month

MIT license

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
~91K SLoC