#logging #pretty #terminal #color

bin+lib prettylog-rs

A pretty and customizable logging library for Rust

3 releases

0.1.2 Aug 24, 2024
0.1.1 Aug 23, 2024
0.1.0 Aug 23, 2024

#276 in Debugging

Download history 368/week @ 2024-08-23 7/week @ 2024-08-30 43/week @ 2024-09-13 17/week @ 2024-09-20 7/week @ 2024-09-27 1/week @ 2024-10-04

82 downloads per month

MIT/Apache

23KB
490 lines

prettylog-rs

Crates.io

A Rust logging library focused on readability in console. prettylog-rs takes advantage of ANSI color codes to make your logs look ✨ pretty ✨.

Installation

Add this to your Cargo.toml:

[dependencies]
prettylog-rs = "0.1.1"

Logging

Logging is very easy, just call the log(message, type) function. The type parameter is optional and defaults to LogType::Runtime.

use prettylog_rs::{log, LogType};

fn main() {
    prettylog_rs::logger::init();

    log("Hello there!", LogType::Information);
    log("general kenobi", LogType::Network);
}

You can also log exceptions!

use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    prettylog_rs::logger::init();

    // Some code that might throw an error
    let result = std::fs::read_to_string("non_existent_file.txt");

    if let Err(e) = result {
        log_exception(&e);
    }

    Ok(())
}

Logger Settings

You can change settings by modifying the LoggerSettings:

use prettylog_rs::logger::{LoggerSettings, LoggerStyle};

fn main() {
    let mut settings = LoggerSettings::get();
    settings.save_to_file = true;
    settings.save_directory_path = String::from("./logs/");
    settings.log_file_name_format = String::from("%Y-%m-%d-%H%M%S");
    settings.logger_style = LoggerStyle::Prefix;
}

Log Types

There are 16 default log types: Debug, Information, Runtime, Network, Success, Warning, Error, Exception, Critical, Audit, Trace, Security, User Action, Performance, Config, and Fatal.

Custom log Types

You can create custom log types by implementing your own enum and associated functions:

use prettylog_rs::types::{AnsiColor, AnsiPair, CustomLogType, LogType};

pub enum MyCustomLogType {
    Cute,
    Git,
    FireWarning,
}

impl CustomLogType for MyCustomLogType {
    fn name(&self) -> &'static str {
        match self {
            MyCustomLogType::Cute => "≽^•⩊•^≼",
            MyCustomLogType::Git => "🤖 Git",
            MyCustomLogType::FireWarning => "🔥 Fire Warning",
        }
    }

    fn color_pair(&self) -> AnsiPair {
        match self {
            MyCustomLogType::Cute => AnsiPair::new(AnsiColor::CutePinkBackground, AnsiColor::CutePink),
            MyCustomLogType::Git => AnsiPair::new(AnsiColor::AquaBackground, AnsiColor::Aqua),
            MyCustomLogType::FireWarning => AnsiPair::new(AnsiColor::OrangeBackground, AnsiColor::Orange),
        }
    }
}

fn main() {
    prettylog_rs::logger::init();

    log("T-This is vewy cuwute message OwO", LogType::Custom(&MyCustomLogType::Cute));
    log("Refusing to merge unrelated histories", LogType::Custom(&MyCustomLogType::Git));
    log("SERVER ROOM ON FIRE, DON'T LET ASO RUN WHILE LOOPS EVER AGAIN", LogType::Custom(&MyCustomLogType::FireWarning));
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Credits

https://github.com/LukynkaCZE https://github.com/LukynkaCZE/PrettyLog

Dependencies

~1.2–2MB
~35K SLoC