| Crates.io | mtlog-tokio |
| lib.rs | mtlog-tokio |
| version | 0.2.1 |
| created_at | 2024-10-16 10:25:45.315658+00 |
| updated_at | 2026-01-03 18:59:54.210779+00 |
| description | Scoped logging for tokio runtimes with support for log files. |
| homepage | https://github.com/AntoineRenaud91/mtlog-rs |
| repository | https://github.com/AntoineRenaud91/mtlog-rs |
| max_upload_size | |
| id | 1411628 |
| size | 35,416 |
Scoped logging for tokio runtimes with per-task configuration and support for log files.
mtlog - Use this instead for non-async, standard multi-threaded applicationsmtlog-progress - Add this for progress bars that work gracefully with mtlogAdd this to your Cargo.toml:
[dependencies]
mtlog-tokio = "0.2"
tokio = { version = "1", features = ["full"] }
use mtlog_tokio::logger_config;
#[tokio::main]
async fn main() {
logger_config()
.scope_global(async move {
log::info!("Hello, world!");
// logs are automatically flushed when scope_global completes
}).await;
}
use mtlog_tokio::logger_config;
#[tokio::main]
async fn main() {
logger_config()
.with_name("main")
.scope_global(async move {
log::info!("Hello, world from main task!");
let handles: Vec<_> = (0..5).map(|i| {
tokio::spawn(async move {
logger_config()
.with_name(&format!("task {i}"))
.scope_local(async move {
log::warn!("Hello, world from task {i}!")
}).await;
})
}).collect();
for h in handles { h.await.unwrap(); }
}).await;
}
Files can be used to log messages. The log file is created if it does not exist and appended to if it does. Tasks can log to different files. If no file is specified in local config, the global file is used.
use mtlog_tokio::logger_config;
#[tokio::main]
async fn main() {
logger_config()
.with_log_file("/tmp/app.log")
.unwrap()
.no_stdout() // disable stdout logging if needed
.scope_global(async move {
log::info!("Hello, world!");
}).await;
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
For detailed API documentation, visit docs.rs/mtlog-tokio.
MIT