| Crates.io | mtlog |
| lib.rs | mtlog |
| version | 0.2.1 |
| created_at | 2024-10-14 09:16:59.995296+00 |
| updated_at | 2026-01-03 18:59:43.368148+00 |
| description | Multi-threaded logger with support for log files. |
| homepage | https://github.com/AntoineRenaud91/mtlog-rs |
| repository | https://github.com/AntoineRenaud91/mtlog-rs |
| max_upload_size | |
| id | 1408110 |
| size | 27,035 |
Multi-threaded logger with per-thread configuration and support for log files.
mtlog-tokio - Use this instead if you're building async applications with tokiomtlog-progress - Add this for progress bars that work gracefully with mtlogAdd this to your Cargo.toml:
[dependencies]
mtlog = "0.2"
use mtlog::logger_config;
let _guard = logger_config()
.init_global();
log::info!("Hello, world!");
⚠️ Warning: the _guard must be kept alive for the entire duration of the program (or the thread when using init_local).
use mtlog::logger_config;
let _guard = logger_config()
.with_name("main")
.init_global();
log::info!("Hello, world from main thread!");
let handles: Vec<_> = (0..5).map(|i| {
std::thread::spawn(move || {
logger_config()
.with_name(&format!("thread {i}"))
.init_local();
log::warn!("Hello, world from thread {i}!")
})
}).collect();
for h in handles { h.join().unwrap(); }
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 mtlog::logger_config;
let _guard = logger_config()
.with_log_file("/tmp/app.log")
.expect("Unable to create log file")
.no_stdout() // disable stdout logging if needed
.init_global();
log::info!("Hello, world!");
drop(_guard); // ensure logs are flushed
assert!(std::fs::read_to_string("/tmp/app.log").unwrap().ends_with("Hello, world!\n"));
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.
For detailed API documentation, visit docs.rs/mtlog.
MIT