LogRoller 🪵
LogRoller is a Rust library for efficient log writing and file rotation. It provides a simple API for writing logs to files and automatically rotating them based on size or time. It works seamlessly with the tracing-appender
crate for use with the tracing
framework.
Table of Contents
- Features
- Installation
- Usage
- Configuration Options
- Advanced Examples
- Performance Considerations
- Troubleshooting
- Contributing
Features ✨
- Log Writing: Write logs to files with ease using standard Write trait implementation
- File Rotation:
- Size-based rotation (bytes, KB, MB, GB)
- Time-based rotation (minutely, hourly, daily)
- Configurable:
- Customizable rotation strategies
- Adjustable file retention policies
- Configurable file permissions
- Time Zones:
- Support for UTC, local, or custom time zones
- Consistent log timing across different deployment environments
- Thread-safe: Designed for safe concurrent usage with proper locking mechanisms
- Tracing Support: Seamless integration with tracing - use LogRoller directly as an appender with tracing-appender
- Compression: Automatic compression for rotated log files (Gzip included, XZ with
xz
feature)
Installation 📦
Add logroller
to your Cargo.toml
:
# For basic logging or with tracing framework
[]
= "0.1"
# Enable XZ compression support (optional)
= { = "0.1", = ["xz"] }
Usage 🚀
1. Use logroller
as a simple logger
Create a new LogRoller
instance and write logs to it:
use ;
use Write;
2. Use logroller
with tracing
and tracing-appender
Integrate with the tracing framework for structured logging:
use ;
use SubscriberInitExt;
Configuration Options 🔧
Rotation Strategies
// Size-based rotation
SizeBased // 1MB in bytes
SizeBased // 1MB in KB
SizeBased // 1MB
SizeBased // 1GB
// Time-based rotation
AgeBased // Rotate every minute
AgeBased // Rotate every hour
AgeBased // Rotate at midnight
Time Zones
use FixedOffset;
// UTC time zone
.time_zone
// Local system time zone
.time_zone
// Custom time zone (e.g., UTC+8)
.time_zone
File Management
// Limit number of kept files
.max_keep_files // Keep only last 7 files
// Add custom suffix to log files
.suffix // Results in: app.log.2025-04-01.error
// Set file permissions (Unix systems only)
.file_mode // rw-r--r-- permissions
Advanced Examples 🔍
High-frequency Logging with Compression
use ;
let logger = new
.rotation
.max_keep_files // Keep one day of hourly logs
.compression
.time_zone // Use UTC for consistent timing
.build?;
Size-based Rotation for Large Files
let logger = new
.rotation
.max_keep_files // Keep only last 5 files
.compression
.build?;
XZ Compression (Feature Flag)
XZ compression provides the highest compression ratios but requires enabling the xz
feature:
// Cargo.toml
// logroller = { version = "0.1", features = ["xz"] }
use ;
let logger = new
.rotation
.compression // Compression level 0-9
.build?;
- Compression levels: 0 (fastest) to 9 (highest compression)
- Trade-offs: Higher compression levels use more CPU and memory
- Best for: Archive logs where compression ratio is more important than speed
Graceful Shutdown
let mut logger = new
.rotation
.graceful_shutdown // Enable graceful shutdown
.build?;
- LogRoller provides graceful shutdown functionality to ensure proper cleanup of background operations: (default is
false
) - When
graceful_shutdown
istrue
,flush()
waits for background compression threads to complete - When
graceful_shutdown
isfalse
(default),flush()
returns immediately without waiting - Setting to
false
prevents potential hangs during shutdown but may cause compression corruption
Performance Considerations 🚀
- Use
tracing-appender::non_blocking
for non-blocking log writes - Enable compression for rotated files to save disk space
- Consider time-based rotation for predictable file sizes
- Use appropriate rotation sizes to balance between file handling overhead and disk usage
- XZ compression: Provides highest compression ratios but uses more CPU and memory. On some low memory devices (etc. Embedded Devices), high ratios may fail to start compression. More info (enable with
xz
feature) - Set reasonable
max_keep_files
to prevent excessive disk usage
Troubleshooting 🔧
Common issues and solutions:
-
File Permissions:
- Ensure the process has write permissions to the log directory
- Use
.file_mode()
to set appropriate permissions on Unix systems
-
Disk Space:
- Monitor disk usage with appropriate
max_keep_files
settings - Enable compression for rotated files
- Use size-based rotation to prevent individual files from growing too large
- Monitor disk usage with appropriate
-
Time Zone Issues:
- Use
TimeZone::UTC
for consistent timing across different environments - Be aware of daylight saving time changes when using local time
- Use
Contributing 🤝
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a new branch for your feature
- Add tests for new functionality
- Submit a pull request
Please ensure your code:
- Follows the existing code style
- Includes appropriate documentation
- Contains tests for new functionality
- Updates the README.md if needed
License 📄
This project is licensed under the MIT License - see the LICENSE file for details.