1 unstable release
Uses new Rust 2024
| 0.1.0 | Oct 26, 2025 |
|---|
#319 in Value formatting
39KB
773 lines
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 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
Dependencies
~2–13MB
~101K SLoC