rslog
A lightweight, zero-dependency logging library for Rust, built entirely using the standard library.
Features
- Zero Dependencies: Built entirely using Rust's standard library
- Multiple Log Levels: Debug, Info, Warn, Error, Critical
- Multiple Output Targets: Console and file output
- Async Writing: Background thread for non-blocking file writes
- Log Rotation: Size-based and time-based rotation
- Multiple Output Formats: Text, JSON, and custom patterns
- Configurable: Flexible configuration options
Quick Start
Add rslog to your Cargo.toml:
[]
= { = "path/to/rslog" }
Basic Usage
use ;
Custom Configuration
use ;
Log Rotation
use ;
Output Formats
Text Format (Default)
[2026-05-09 10:30:45.123456789] [INFO] Application started
JSON Format
Custom Format
use ;
let config = new
.output_format
.build;
Custom Format Placeholders
| Placeholder | Description | Example |
|---|---|---|
%d |
Full timestamp | 2026-05-09 10:30:45.123456789 |
%D |
Date | 2026-05-09 |
%T |
Time | 10:30:45.123456789 |
%p |
Full log level | DEBUG, INFO, WARN, ERROR, CRITICAL |
%P |
Short log level | D, I, W, E, C |
%m |
Log message | User-provided message |
%n |
Newline | \n |
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
log_dir |
String | "logs" |
Directory for log files |
file_prefix |
String | "serial_" |
Prefix for log file names |
file_extension |
String | ".log" |
File extension |
console_enabled |
bool | true |
Enable console output |
level |
LogLevel | Debug |
Minimum log level |
output_format |
OutputFormat | Text |
Output format |
rotation |
Option<RotatorConfig> | Default | Log rotation config |
console_colors |
bool | true |
Enable ANSI colors in console output |
color_scheme |
LogColorScheme | Default | Color scheme for log levels |
Log Levels
| Level | Priority | Description |
|---|---|---|
Debug |
1 | Detailed debug information |
Info |
2 | General information |
Warn |
3 | Potential issues |
Error |
4 | Recoverable errors |
Critical |
5 | Unrecoverable errors |
Color Support
rslog supports ANSI color codes for console output, making log levels easily distinguishable.
Default Color Scheme
| Log Level | Default Color | ANSI Code |
|---|---|---|
Debug |
Cyan | \x1b[36m |
Info |
Green | \x1b[32m |
Warn |
Yellow | \x1b[33m |
Error |
Red | \x1b[31m |
Critical |
Bright Red | \x1b[91m |
Custom Color Configuration
use ;
// Create a custom color scheme
let custom_scheme = new;
let config = new
.color_scheme
.console_colors // Enable colors
.build;
Available Colors
use Color;
// Basic colors
Black
Red
Green
Yellow
Blue
Magenta
Cyan
White
// Bright colors
BrightBlack
BrightRed
BrightGreen
BrightYellow
BrightBlue
BrightMagenta
BrightCyan
BrightWhite
Runtime Color Control
use Logger;
let logger = get_instance;
// Enable/disable colors at runtime
logger.set_console_colors; // Enable colors
logger.set_console_colors; // Disable colors
Notes
- Colors are only applied to console output, not to file output
- Colors can be disabled globally via configuration
- Color support is automatically detected (basic terminal detection)
- Use
logger.set_console_colors(false)for non-terminal environments
Examples
Run the basic example:
Run the color example:
Documentation
Generate documentation:
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.
中文文档
查看中文文档:docs/zh-CN/README.md