Skip to content

Anmsh27/rs-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

A logging library implemented in Rust

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages