ccb 0.1.1

A beautiful, terminal-focused structured logger inspired by charmbracelet/log
Documentation

๐ŸŽจ CCB Logger

Rust Latest version Documentation License

๐Ÿš€ A beautiful, terminal-focused structured logger for Rust

CCB brings elegance and visual appeal to the Rust ecosystem. It is designed for command-line interface (CLI) applications that want to achieve beautiful, readable, and structured log output.

โœจ Features

  • ๐ŸŽฏ Semantic Log Levels: Trace, Debug, Info, Warn, Error with four-character alignment
  • ๐ŸŒˆ Automatic Colors: Beautiful colored output with smart terminal detection
  • โฐ Precise Timestamps: High-precision timestamps in 2006-01-02 03:04:05.789 format
  • ๐Ÿ”— Chainable Context: Add structured key-value pairs with with(key, value)
  • ๐Ÿ› ๏ธ Simple Macros: Easy-to-use macros with variadic arguments support
  • ๐ŸŽ›๏ธ Global Logger: Set and use a global logger instance across your application
  • ๐Ÿ“ฑ Terminal Friendly: No icons, maximum compatibility across terminals
  • โšก Zero Config: Works beautifully out of the box with sensible defaults

๐Ÿš€ Quick Start

Add CCB Logger to your Cargo.toml:

[dependencies]
ccb = "0.1.0"

Basic Usage

use ccb::{info, warn, error, debug, trace};

fn main() {
    // Simple logging
    info!("Application started");
    warn!("This is a warning");
    error!("Something went wrong");
    
    // With structured fields
    info!("User login", "user_id", "12345", "ip", "192.168.1.100");
    error!("Database error", "table", "users", "error", "connection timeout");
}

Custom Logger Configuration

use ccb::{Logger, Level, set_global_logger};

fn main() {
    // Create a custom logger
    let logger = Logger::new()
        .with_level(Level::Debug)
        .with_colors(true)
        .with_timestamp(true)
        .with("service", "my-app")
        .with("version", "1.0.0");
    
    // Set as global logger
    set_global_logger(logger);
    
    // Now all macro calls will use the configured logger
    debug!("Debug message with context");
    info!("Request processed", "method", "GET", "path", "/api/users");
}

Advanced Usage

use ccb::{Logger, Level, Config};

fn main() {
    // Custom configuration
    let config = Config {
        level: Level::Trace,
        use_colors: false,  // Disable colors for CI/CD
        show_timestamp: true,
    };
    
    let logger = Logger::with_config(config)
        .with("component", "auth")
        .with("environment", "production");
    
    // Direct logger usage
    logger.trace("Entering function", &[("fn", "authenticate")]);
    logger.info("Authentication successful", &[("user", "alice")]);
    logger.error("Rate limit exceeded", &[("ip", "192.168.1.1"), ("attempts", "10")]);
}

๐Ÿ“Š Output Examples

2024-01-15 14:30:25.1234 INFO Application started
2024-01-15 14:30:25.1235 WARN Configuration file not found path=config.toml
2024-01-15 14:30:25.1236 INFO User login user_id=12345 ip=192.168.1.100
2024-01-15 14:30:25.1237 ERRO Database connection failed error=timeout retry_count=3
2024-01-15 14:30:25.1238 DEBG Cache hit key=user:12345 ttl=300

๐ŸŽฏ Log Levels

CCB supports five log levels with four-character alignment:

Level Code Color Description
Trace TRCE Cyan ๐Ÿ” Detailed tracing information
Debug DEBG Blue ๐Ÿ› Debug information for developers
Info INFO Green โ„น๏ธ General information messages
Warn WARN Yellow โš ๏ธ Warning messages
Error ERRO Red โŒ Error conditions

๐Ÿ”ง Configuration Options

Logger Methods

  • with_level(level) - Set minimum log level
  • with_colors(bool) - Enable/disable colored output
  • with_timestamp(bool) - Show/hide timestamps
  • with(key, value) - Add context key-value pair

Environment Detection

CCB automatically detects if output is going to a terminal and enables colors accordingly. You can override this behavior:

let logger = Logger::new().with_colors(false); // Force disable colors

๐Ÿงช Testing

Run the test suite:

cargo test

Run tests with output:

cargo test -- --nocapture

๐Ÿ“š Examples

Check out the examples/ directory for more usage patterns:

cargo run --example basic_usage

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

  1. ๐Ÿด Fork the repository
  2. ๐ŸŒŸ Create your feature branch (git checkout -b feature/amazing-feature)
  3. โœ… Commit your changes (git commit -m 'Add some amazing feature')
  4. ๐Ÿ“ค Push to the branch (git push origin feature/amazing-feature)
  5. ๐Ÿ”„ Open a Pull Request

๐Ÿ“„ License

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

๐Ÿ™ Acknowledgments

  • Inspired by charmbracelet/log โค๏ธ
  • CCB has no real meaning, the name was given by a friend
  • Built with โค๏ธ for the Rust community
  • Thanks to all contributors! ๐ŸŽ‰

๐Ÿ”— Related Projects