A simple, colorful, and flexible logging library for Rust with support for timestamps, log levels, custom error codes, and customizable ANSI colors. Built using a builder pattern for ergonomic usage.
- Four log levels:
Info,Warn,Error,Debug - Optional timestamps with custom format via
timestamp_format("%H:%M:%S") - Optional custom error codes appended as
(code 123) - Color-coded output in the terminal with:
- Global disable via
.no_color() - Full custom color map via
.colors(AnsiColors { .. }) - Per-level override via
.color_for_level(Level::Error, "\x1b[97;41m")
- Global disable via
- Smart output stream selection:
WarnandErrortostderr, others tostdout - Ergonomic builder pattern
Add code_logger to your Cargo.toml:
[dependencies]
code_logger = "0.1"use code_logger::log; // if used as a library crate
fn main() {
// Basic with timestamp and code
log("Hello, world!".to_string())
.timestamp()
.code(42)
.info()
.print();
}use code_logger::logger::{log, AnsiColors, Level};
fn main() {
// Custom timestamp format
log("Custom time format".to_string())
.timestamp_format("%H:%M:%S")
.debug()
.print();
// Disable colors entirely (useful for logs to file)
log("No colors here".to_string())
.no_color()
.warn()
.print();
// Provide custom colors
let mut custom = AnsiColors::default();
custom.info = "\x1b[35m".to_string(); // magenta
custom.warn = "\x1b[36m".to_string(); // cyan
log("Custom color config".to_string())
.colors(custom)
.info()
.print();
// Override a single level color quickly
log("Only ERROR is white on red".to_string())
.color_for_level(Level::Error, "\x1b[97;41m")
.error()
.print();
}log(message: String) -> LoggerBuilderLoggerBuildermethods:.code(i32).timestamp().timestamp_format(fmt: &str).no_color().colors(AnsiColors).color_for_level(Level, &str).info()/.warn()/.error()/.debug()→ returnsLogger
Logger::print()to emit the log immediately
- ANSI colors are commonly supported by terminals. Use
.no_color()when redirecting output to files. - Default timestamp format is
%Y-%m-%d %H:%M:%S. Warn/Errorgo tostderr, which helps when piping or filtering output.