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:
[]
= "0.1.0"
Basic usage:
use Pretint;
let logger = new;
logger.info;
logger.info_with_fields;
Examples
Basic Logging
use ;
let logger = new;
logger.trace;
logger.debug;
logger.info;
logger.warn;
logger.error;
Structured Logging
let logger = new;
logger.info_with_fields;
Child Loggers
let logger = new;
let api_logger = logger.child_with_fields;
api_logger.info;
api_logger.info_with_fields;
Performance Timing
// Manual timing
let timer = logger.timer_with_label;
// ... perform operation ...
logger.info_with_fields;
// Automatic timing
Different Output Formats
use ;
// Pretty format (default)
let pretty_logger = new;
// JSON format
let json_logger = with_config;
// Compact format
let compact_logger = with_config;
Using Macros
use ;
let logger = new;
// Basic macros
info!;
warn!;
error!;
// Field macros
info_fields!;
// Timer macros
Configuration
use ;
let config = Config ;
let logger = with_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
Compact Format
INFO Hello, world!
INFO User logged in user_id=123 ip=192.168.1.1
Running Examples
# Run the main demo
# Run specific examples
API Reference
Core Types
Pretint: Main logger structLevel: Log levels (Trace, Debug, Info, Warn, Error)OutputFormat: Output formats (Json, Pretty, Compact)Config: Logger configurationTimer: Manual timing utilityTimerGuard: Automatic timing utility
Main Methods
Pretint::new(): Create a new logger with default configPretint::with_config(config): Create a logger with custom configlogger.child(): Create a child loggerlogger.child_with_fields(fields): Create a child logger with fieldslogger.add_field(key, value): Add a field to the loggerlogger.add_fields(fields): Add multiple fields to the logger
Logging Methods
logger.trace(message): Log at trace levellogger.debug(message): Log at debug levellogger.info(message): Log at info levellogger.warn(message): Log at warn levellogger.error(message): Log at error levellogger.trace_with_fields(message, fields): Log with fieldslogger.debug_with_fields(message, fields): Log with fieldslogger.info_with_fields(message, fields): Log with fieldslogger.warn_with_fields(message, fields): Log with fieldslogger.error_with_fields(message, fields): Log with fields
Timing Methods
logger.timer(): Create a manual timerlogger.timer_with_label(label): Create a labeled timerlogger.timer_guard(level): Create an automatic timer guardlogger.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