chromalog

Crates.iochromalog
lib.rschromalog
version0.0.2
sourcesrc
created_at2024-10-11 21:11:18.148915
updated_at2024-10-11 22:03:16.545392
descriptionA customizable logger with dynamic color coding and file logging.
homepage
repository
max_upload_size
id1405849
size21,777
(burnmandont)

documentation

README

ChromaLog

ChromaLog is a customizable, colorful logger for Rust applications. It enables dynamic color coding of log levels and supports writing log messages to both the console and an optional log file. You can define your own color scheme for different log levels, including ERROR, WARN, INFO, DEBUG, and TRACE.

Features

  • Customizable Color Scheme: Define colors for log levels and other components such as the log target or timestamp.
  • File Logging: Optionally write log messages to a specified log file while still printing them to the console.
  • Supports All Log Levels: ERROR, WARN, INFO, DEBUG, and TRACE.
  • Timestamped Logs: Logs include date and time with millisecond precision.
  • Console and File Output: Log messages are simultaneously printed to the console and can be saved to a log file.
  • Simple Integration: Easily add it to your project using the log crate's interface.

Installation

Add the following to your Cargo.toml:

[dependencies]
chromalog = "0.1.0"
log = "0.4"
colored = "2.0"
chrono = "0.4"

## Example Usage 
use chromalog::{ChromaLog, ColorConfig, ArgColor, error, warn, info, debug, trace, Color, LevelFilter};
use std::fs::File;
use std::sync::{Arc, Mutex};

fn main() {
    // Create a log file (optional)
    let log_file = File::create("my_log.log").expect("Unable to create log file");
    let log_file = Arc::new(Mutex::new(log_file));

    // Define custom colors for log levels
    let custom_colors = ColorConfig {
        error_color: Color::Red,
        warn_color: Color::BrightYellow,
        info_color: Color::BrightGreen,
        debug_color: Color::BrightBlue,
        trace_color: Color::BrightMagenta,
        arg_color: None, // ArgColor::Fixed(Color::BrightMagenta), ArgColor::LogLevel
        target_color: Color::Cyan,
        datetime_color: None, // Use default color for datetime
    };

    // Initialize the logger with a custom color scheme and log file
    ChromaLog::init(LevelFilter::Trace, custom_colors, Some(log_file)).unwrap();

    // Log messages at different levels
    error!("This is an error message.");
    warn!("This is a warning message.");
    info!("This is an info message.");
    debug!("This is a debug message.");
    trace!("This is a trace message.");
}

Commit count: 0

cargo fmt