Crates.io | prettylog-rs |
lib.rs | prettylog-rs |
version | 0.1.2 |
source | src |
created_at | 2024-08-23 21:08:14.358711 |
updated_at | 2024-08-24 06:04:40.229623 |
description | A pretty and customizable logging library for Rust |
homepage | |
repository | https://github.com/aubreyrs/prettylog-rs |
max_upload_size | |
id | 1349628 |
size | 42,886 |
A Rust logging library focused on readability in console. prettylog-rs takes advantage of ANSI color codes to make your logs look ✨ pretty ✨.
Add this to your Cargo.toml
:
[dependencies]
prettylog-rs = "0.1.1"
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(())
}
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;
}
There are 16 default log types: Debug, Information, Runtime, Network, Success, Warning, Error, Exception, Critical, Audit, Trace, Security, User Action, Performance, Config, and Fatal.
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));
}
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
https://github.com/LukynkaCZE https://github.com/LukynkaCZE/PrettyLog