firo_logger
A high-performance, feature-rich logger for Rust applications with colored output, structured logging, file rotation, async logging, and advanced configuration.
Features
- ✨ Colored console output with customizable colors
- 📊 Structured logging with JSON format support
- 📁 File logging with automatic rotation (size-based and time-based)
- ⚡ Async logging for high-performance applications
- 🎯 Level filtering with module-specific filters
- 🔒 Thread-safe with minimal overhead
- 📍 Caller information (file, line, module)
- 🏷️ Custom metadata support
- 🌍 Environment configuration support
- 🏗️ Builder pattern for easy configuration
- 🔄 Log rotation with configurable retention
- 🚀 Performance optimized with buffering and batching
- 📈 Statistics and monitoring built-in
- 🔧 Backwards compatibility with legacy API
Quick Start
Add this to your Cargo.toml:
[]
= "0.3.0"
Basic Usage
use ;
Advanced Configuration
use ;
Configuration Options
Environment Variables
The logger can be configured using environment variables:
FIRO_LOG_LEVEL: Set log level (ERROR,WARNING,INFO,SUCCESS,DEBUG)FIRO_LOG_FILE: Set log file pathFIRO_LOG_FORMAT: Set output format (text,json,plain)NO_COLOR: Disable colored outputFORCE_COLOR: Force colored output even when not in a terminal
use init_from_env;
Configuration Builder
use ;
let config = builder
// Basic settings
.level
.format
// Console output
.console
.colors
.use_stderr // Use stderr for errors/warnings
// File output
.file
.rotate_by_size // 50MB, keep 10 files
.rotate_by_time // Daily rotation, keep 7 days
// Performance
.async_logging // Enable async with 1000 message buffer
// Metadata and context
.include_caller
.include_thread
.datetime_format
.metadata
.metadata
// Module-specific filtering
.module_filter
.module_filter
.build;
Logging Macros
Basic Logging
log_error!;
log_warning!;
log_info!;
log_success!;
log_debug!;
Advanced Logging
Structured Logging with Metadata
use ;
log_with_metadata!;
Conditional Logging
use ;
let debug_mode = var.is_ok;
log_if!;
let should_trace = user.is_admin;
log_if!;
Rate-Limited Logging
use ;
use Duration;
// In a high-frequency loop
for i in 0..10000
Function Tracing
use trace_function;
Performance Timing
use ;
let result = time_block!;
Assert with Logging
use log_assert;
let user_id = get_user_id;
log_assert!;
// Debug-only assertions
log_debug_assert!;
Output Formats
Text Format (Default)
2024-08-14 09:33:45.123 [ ERROR]: Database connection failed: timeout
2024-08-14 09:33:45.124 [WARNING]: Deprecated API endpoint used
2024-08-14 09:33:45.125 [ INFO]: User alice logged in
2024-08-14 09:33:45.126 [SUCCESS]: Payment of $49.99 processed
2024-08-14 09:33:45.127 [ DEBUG]: Request processed in 23ms
JSON Format
Plain Format
2024-08-14 09:33:45 [ERROR]: Database connection failed: timeout
2024-08-14 09:33:45 [INFO]: User alice logged in
File Rotation
Size-Based Rotation
let config = builder
.file
.rotate_by_size // 10MB files, keep 5
.build;
Generated files:
app.log(current)app.log.1628934123(backup)app.log.1628934100(backup)- etc.
Time-Based Rotation
use RotationFrequency;
let config = builder
.file
.rotate_by_time // Daily rotation, keep 7 days
.build;
Generated files:
app.log(current)app.log.2024-08-13(yesterday)app.log.2024-08-12(day before)- etc.
Async Logging
For high-performance applications, enable async logging:
let config = builder
.async_logging // Buffer up to 10,000 messages
.build;
// Logging calls return immediately, processing happens in background
for i in 0..1000000
Integration with log Crate
Enable the log feature and use firo_logger as a backend:
[]
= { = "0.3.0", = ["log"] }
= "0.4"
use init_with_log;
Performance
firo_logger is designed for high performance:
- Async logging: Non-blocking log calls with background processing
- Buffered I/O: Configurable buffer sizes for file output
- Lazy formatting: Log messages are only formatted if they pass level filters
- Zero allocation: Many operations avoid memory allocation
- Lock-free paths: Optimized for concurrent access
Benchmarks show firo_logger can handle:
- 1M+ logs/second in async mode
- Sub-microsecond latency for filtered-out messages
- Minimal memory overhead
Migration from v0.2.x
The new version is fully backwards compatible. Your existing code will continue to work:
// Old API still works
use ;
log;
error;
But we recommend migrating to the new API:
// New API
use ;
init_default?;
log_info!?;
log_error!?;
Examples
See the examples/ directory for more examples:
# Basic usage
# Advanced features
# Performance testing
Features Flags
colors(default): ANSI color supportjson(default): JSON output format supportasync(default): Async logging supportlog: Integration with the standardlogcratesyslog: Syslog output support (future feature)
# Minimal build without async support
[]
= { = "0.3.0", = false, = ["colors"] }
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.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
v0.3.0 (2024-08-14)
- 🎉 Major rewrite with backwards compatibility
- ✨ New Features:
- Structured logging with JSON support
- File rotation (size and time-based)
- Async logging for high performance
- Module-specific log level filtering
- Advanced macros (rate limiting, timing, tracing)
- Environment variable configuration
- Statistics and monitoring
- Integration with standard
logcrate
- 🚀 Performance: Up to 10x faster than v0.2.x
- 🔧 API: New builder pattern for configuration
- 📚 Documentation: Comprehensive examples and guides
v0.2.1 (Previous)
- Basic colored console logging
- Simple file output
- Basic log levels
Acknowledgments
- Inspired by popular logging libraries like
env_logger,slog, andtracing - Built with performance and usability in mind
- Thanks to the Rust community for feedback and contributions