pretint

Crates.iopretint
lib.rspretint
version0.1.0
created_at2025-10-26 18:02:41.78205+00
updated_at2025-10-26 18:02:41.78205+00
descriptionA pretty print library for Rust with multiple APIs and easy usage
homepage
repositoryhttps://github.com/techsakan/pretint
max_upload_size
id1901676
size69,973
Jarupak Srisuchat (ppenter)

documentation

README

Pretint - Pretty Print Library for Rust

A comprehensive logging library inspired by Pino but designed for Rust with multiple APIs and easier usage. Features structured logging, multiple output formats, performance timing, and convenient macros.

Features

  • 🎨 Multiple Output Formats: JSON, Pretty (colored), and Compact
  • 📊 Structured Logging: Add fields and metadata to your logs
  • 🌳 Child Loggers: Create child loggers with inherited context
  • ⏱️ Performance Timing: Built-in timing utilities and automatic measurement
  • 🎯 Convenient Macros: Easy-to-use macros for common logging patterns
  • 🎨 Colored Output: Beautiful colored output for better readability
  • 📝 Multiple Log Levels: Trace, Debug, Info, Warn, Error
  • 🔧 Configurable: Customize level, format, colors, and more

Quick Start

Add Pretint to your Cargo.toml:

[dependencies]
pretint = "0.1.0"

Basic usage:

use pretint::Pretint;

let logger = Pretint::new();
logger.info("Hello, world!");
logger.info_with_fields("User logged in", &[("user_id", "123"), ("ip", "192.168.1.1")]);

Examples

Basic Logging

use pretint::{Pretint, Level};

let logger = Pretint::new();
logger.trace("Trace message");
logger.debug("Debug message");
logger.info("Info message");
logger.warn("Warning message");
logger.error("Error message");

Structured Logging

let logger = Pretint::new();
logger.info_with_fields("User action", &[
    ("user_id", "12345"),
    ("action", "login"),
    ("ip_address", "192.168.1.100"),
]);

Child Loggers

let logger = Pretint::new();
let api_logger = logger.child_with_fields(&[
    ("service", "api"),
    ("version", "v1.0"),
]);

api_logger.info("API request received");
api_logger.info_with_fields("Request processed", &[
    ("method", "GET"),
    ("path", "/api/users"),
    ("status_code", "200"),
]);

Performance Timing

// Manual timing
let timer = logger.timer_with_label("database_query");
// ... perform operation ...
logger.info_with_fields("Query completed", &[
    ("duration_ms", &timer.elapsed_ms().to_string()),
]);

// Automatic timing
{
    let _guard = logger.timer_guard_with_label(Level::Info, "file_processing");
    // ... perform operation ...
    // Timing will be logged automatically when guard is dropped
}

Different Output Formats

use pretint::{Pretint, OutputFormat, Config};

// Pretty format (default)
let pretty_logger = Pretint::new();

// JSON format
let json_logger = Pretint::with_config(Config {
    format: OutputFormat::Json,
    ..Default::default()
});

// Compact format
let compact_logger = Pretint::with_config(Config {
    format: OutputFormat::Compact,
    ..Default::default()
});

Using Macros

use pretint::{Pretint, info, warn, error, info_fields, timer};

let logger = Pretint::new();

// Basic macros
info!(logger, "Application started with version {}", "1.0.0");
warn!(logger, "Deprecated API endpoint used: {}", "/api/v1/users");
error!(logger, "Failed to connect to database: {}", "connection timeout");

// Field macros
info_fields!(logger, "User login attempt", &[
    ("user_id", "12345"),
    ("ip_address", "192.168.1.100"),
]);

// Timer macros
{
    let _guard = timer!(logger, Level::Info);
    // ... perform operation ...
    // Timer will be logged automatically
}

Configuration

use pretint::{Pretint, Level, OutputFormat, Config};

let config = Config {
    level: Level::Debug,
    format: OutputFormat::Json,
    enable_colors: false,
    include_timestamp: true,
    include_logger_id: false,
};

let logger = Pretint::with_config(config);

Output Formats

Pretty Format (Default)

[2024-01-01 12:00:00.123] INFO Hello, world!
[2024-01-01 12:00:00.124] INFO User logged in user_id=123 ip=192.168.1.1

JSON Format

{"timestamp":"2024-01-01T12:00:00.123Z","level":"info","message":"Hello, world!","fields":{},"logger_id":"123e4567-e89b-12d3-a456-426614174000"}
{"timestamp":"2024-01-01T12:00:00.124Z","level":"info","message":"User logged in","fields":{"user_id":"123","ip":"192.168.1.1"},"logger_id":"123e4567-e89b-12d3-a456-426614174000"}

Compact Format

INFO Hello, world!
INFO User logged in user_id=123 ip=192.168.1.1

Running Examples

# Run the main demo
cargo run

# Run specific examples
cargo run --example basic
cargo run --example timing
cargo run --example formats
cargo run --example macros

API Reference

Core Types

  • Pretint: Main logger struct
  • Level: Log levels (Trace, Debug, Info, Warn, Error)
  • OutputFormat: Output formats (Json, Pretty, Compact)
  • Config: Logger configuration
  • Timer: Manual timing utility
  • TimerGuard: Automatic timing utility

Main Methods

  • Pretint::new(): Create a new logger with default config
  • Pretint::with_config(config): Create a logger with custom config
  • logger.child(): Create a child logger
  • logger.child_with_fields(fields): Create a child logger with fields
  • logger.add_field(key, value): Add a field to the logger
  • logger.add_fields(fields): Add multiple fields to the logger

Logging Methods

  • logger.trace(message): Log at trace level
  • logger.debug(message): Log at debug level
  • logger.info(message): Log at info level
  • logger.warn(message): Log at warn level
  • logger.error(message): Log at error level
  • logger.trace_with_fields(message, fields): Log with fields
  • logger.debug_with_fields(message, fields): Log with fields
  • logger.info_with_fields(message, fields): Log with fields
  • logger.warn_with_fields(message, fields): Log with fields
  • logger.error_with_fields(message, fields): Log with fields

Timing Methods

  • logger.timer(): Create a manual timer
  • logger.timer_with_label(label): Create a labeled timer
  • logger.timer_guard(level): Create an automatic timer guard
  • logger.timer_guard_with_label(level, label): Create a labeled timer guard

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Author

Created by techsakan

Commit count: 0

cargo fmt