| Crates.io | fstdout-logger |
| lib.rs | fstdout-logger |
| version | 0.1.1 |
| created_at | 2025-05-12 12:31:12.991733+00 |
| updated_at | 2025-05-16 08:47:46.765385+00 |
| description | An implementation of the log crate that logs to stdout and to an optional log file with configurable options. |
| homepage | |
| repository | https://github.com/LOSEARDES77/fstdout-logger |
| max_upload_size | |
| id | 1670521 |
| size | 63,472 |
A simple and flexible dual-destination logger for Rust applications that implements the log crate's API.
log crate macrosAdd this to your Cargo.toml:
[dependencies]
fstdout-logger = "0.1.0"
log = "0.4"
The crate uses the following dependencies internally:
colored for terminal coloringchrono for timestamp formattingthiserror for error handlinguse fstdout_logger::init_logger;
use log::{debug, error, info, warn};
fn main() {
// Initialize logger with defaults (colored output, Info level)
if let Err(e) = init_logger(Some("application.log")) {
eprintln!("Failed to initialize logger: {}", e);
return;
}
// Now use the standard log macros
info!("Application started"); // Blue
debug!("This won't show by default"); // Suppressed (below Info level)
warn!("Warning: resource usage high"); // Yellow
error!("Failed to process item: {}", "invalid format"); // Red
}
For common use cases, presets are available:
use fstdout_logger::{init_development_logger, init_production_logger};
use log::{debug, error, info};
fn main() {
// For development: Debug level with file info and colors
init_development_logger(Some("dev.log")).expect("Failed to initialize logger");
// Or for production: Info level without file info, concise timestamps
// init_production_logger(Some("app.log")).expect("Failed to initialize logger");
debug!("Debug info shows in development mode"); // Shows in development, hidden in production
info!("Application running");
error!("Something went wrong!");
}
For full control, use the configuration builder:
use fstdout_logger::{init_logger_with_config, LoggerConfig, LoggerConfigBuilder};
use log::{info, LevelFilter};
fn main() {
// Create a custom configuration
let config = LoggerConfig::builder()
.level(LevelFilter::Info)
.show_file_info(false) // Hide file and line info for cleaner output
.show_date_in_stdout(false) // Show only time in stdout (HH:MM:SS)
.use_colors(true) // Enable colored output
.build();
// Initialize with the custom config
init_logger_with_config(Some("application.log"), config)
.expect("Failed to initialize logger");
info!("Customized logging experience!");
}
By default, terminal output shows a concise format with colors:
[HH:MM:SS LEVEL file:line] Message
For example:
[14:23:45 INFO main.rs:25] Application started
Log messages are colored according to log level:
ERROR: Bold RedWARN: Bold YellowINFO: Bold BlueDEBUG: GreenTRACE: Default terminal colorThe timestamp and file information are displayed in a dimmed color to make the log level and message stand out.
Log messages in files always include the date and file information:
[YYYY-MM-DD HH:MM:SS LEVEL file:line] Message
For example:
[2023-05-15 14:23:45 INFO main.rs:25] Application started
You can configure the output format through the LoggerConfig:
show_file_info - Toggle display of file and line informationshow_date_in_stdout - Toggle inclusion of date in terminal outputuse_colors - Enable or disable colored output in terminallevel - Set the minimum log level to displayThe crate includes examples that demonstrate its usage:
# Simple logging with custom configuration
cargo run --example basic_usage
# Stdout-only logging with minimal format
cargo run --example stdout_only
# Comparing different color and format options
cargo run --example color_options
# Demonstrating production vs development presets
cargo run --example production
The logger provides several initialization functions for different use cases:
init_logger(path) - Simple initialization with defaultsinit_logger_with_level(path, level) - Set a specific log levelinit_logger_with_config(path, config) - Use a custom configurationinit_production_logger(path) - Use production-optimized settingsinit_development_logger(path) - Use development-optimized settingsinit_stdout_logger(config) - Initialize a stdout-only loggerinit_simple_stdout_logger(level) - Initialize a minimal stdout-only loggerThis project is licensed under the MIT License - see the LICENSE file for details.