| Crates.io | pretint |
| lib.rs | pretint |
| version | 0.1.0 |
| created_at | 2025-10-26 18:02:41.78205+00 |
| updated_at | 2025-10-26 18:02:41.78205+00 |
| description | A pretty print library for Rust with multiple APIs and easy usage |
| homepage | |
| repository | https://github.com/techsakan/pretint |
| max_upload_size | |
| id | 1901676 |
| size | 69,973 |
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.
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")]);
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");
let logger = Pretint::new();
logger.info_with_fields("User action", &[
("user_id", "12345"),
("action", "login"),
("ip_address", "192.168.1.100"),
]);
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"),
]);
// 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
}
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()
});
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
}
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);
[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
{"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"}
INFO Hello, world!
INFO User logged in user_id=123 ip=192.168.1.1
# 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
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 utilityPretint::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 loggerlogger.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 fieldslogger.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 guardMIT License - see LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
Created by techsakan