#log-file #log #console #file #logger #logging #colored

chromalog

A customizable logger with dynamic color coding and file logging

2 releases

0.0.2 Oct 11, 2024
0.0.1 Oct 11, 2024

#685 in Development tools

Download history 289/week @ 2024-10-08 46/week @ 2024-10-15

335 downloads per month

MIT license

22KB
135 lines

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.");
}

Dependencies

~1–11MB
~61K SLoC