Rust-Loguru
A flexible and efficient logging library for Rust inspired by Python's Loguru. Designed to provide an intuitive, powerful logging experience while maintaining Rust's performance characteristics.
Features
- Multiple log levels: TRACE, DEBUG, INFO, SUCCESS, WARNING, ERROR, CRITICAL
- Thread-safe global logger: Safe to use in multi-threaded applications
- Extensible handler system: Console, file, and custom handlers
- Configurable log formatting: Customize how log messages are displayed
- Support for metadata in log records: Add structured data to log messages
- Convenient logging macros: Easy to use macros for all log levels
- File rotation: Automatic file rotation based on size with retention policies
- Colorized output: Colorful console output for better readability
- Source location capture: Automatically capture file, line, and module information
Installation
Add this to your Cargo.toml
:
[]
= "0.1.0"
Quick Start
use ;
use ConsoleHandler;
use Arc;
use RwLock;
Core Concepts
Log Levels
Rust-Loguru supports the following log levels, in order of increasing severity:
- TRACE: Very detailed information, typically only useful for debugging specific issues
- DEBUG: Detailed information useful for debugging
- INFO: General information about the application's operation
- SUCCESS: Successful operations (similar to INFO but indicates success)
- WARNING: Potential issues that don't prevent the application from working
- ERROR: Errors that prevent specific operations from working
- CRITICAL: Critical errors that may lead to application failure
Handlers
Handlers determine where log messages are sent. Rust-Loguru comes with:
- ConsoleHandler: Outputs logs to stdout or stderr
- FileHandler: Writes logs to a file with optional rotation
- NullHandler: Discards all logs (useful for testing)
Creating a Console Handler
use ConsoleHandler;
use LogLevel;
// Output to stderr with INFO level
let handler = stderr
.with_colors
.with_pattern;
Creating a File Handler
use FileHandler;
use LogLevel;
use Path;
// Write to a file with rotation at 10MB and keep 5 old files
let handler = new.unwrap
.with_rotation
.with_retention
.with_colors;
Logging with Metadata
You can add structured metadata to log records:
use ;
// Using the Record API directly
let record = new
.with_metadata
.with_metadata;
log;
// Or using the macro
log_with_metadata!;
Customizing Formatters
Customize how logs are formatted:
use Formatter;
let formatter = new
.with_colors
.with_timestamp
.with_level
.with_module
.with_location
.with_pattern;
Configuration Presets
Use built-in configuration presets:
use LoggerConfig;
// Development configuration with detailed logging
let config = development;
// Production configuration with minimal logging
let config = production;
Advanced Usage
Creating Custom Handlers
Implement the Handler
trait to create custom handlers:
use Handler;
use LogLevel;
use Record;
use Formatter;
Structured Logging with JSON
Use the structured data capability:
use ;
use json;
let record = new
.with_structured_data
.unwrap;
log;
Performance Considerations
- Log records below the configured level are filtered out early for minimal overhead
- File handlers use buffered I/O for efficient disk operations
- Consider using appropriate log levels in production to minimize overhead
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under either of:
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.