mtlog
Multi-threaded logger with per-thread configuration and support for log files.
Related Crates
mtlog-tokio- Use this instead if you're building async applications with tokiomtlog-progress- Add this for progress bars that work gracefully with mtlog
Installation
Add this to your Cargo.toml:
[]
= "0.2"
Usage
Basic Usage
use logger_config;
let _guard = logger_config
.init_global;
info!;
⚠️ Warning: the _guard must be kept alive for the entire duration of the program (or the thread when using init_local).
Multi-threaded Logging
use logger_config;
let _guard = logger_config
.with_name
.init_global;
info!;
let handles: = .map.collect;
for h in handles
Logging to Files
Files can be used to log messages. The log file is created if it does not exist and appended to if it does. Different threads can log to different files. If no file is specified in local config, the global file is used.
use logger_config;
let _guard = logger_config
.with_log_file
.expect
.no_stdout // disable stdout logging if needed
.init_global;
info!;
drop; // ensure logs are flushed
assert!;
Configuration
Flush Interval
For performance optimization, file logging uses batched writes that are flushed periodically. This can be configured via the MTLOG_FLUSH_INTERVAL_MS environment variable.
Default: 100ms
Note: Stdout logging always flushes immediately to ensure progress bars display correctly and logs appear in real-time.
Documentation
For detailed API documentation, visit docs.rs/mtlog.
License
MIT