mtlog-tokio
Scoped logging for tokio runtimes with support for log files.
Usage
// Cargo.toml
...
[dependencies]
mtlog-tokio = "0.2.0"
tokio = {version = "1.40.0", features = ["full"]}
use mtlog_tokio::logger_config;
#[tokio::main]
async fn main() {
logger_config()
.scope_global(async move {
log::info!("Hello, world!");
}).await;
}
Multi-threaded logging
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 thread!");
let handles: Vec<_> = (0..5).map(|i| {
tokio::spawn(async move {
logger_config()
.with_name(&format!("thread {i}"))
.scope_local(async move {
log::warn!("Hello, world from thread {i}!")
}).await;
})
}).collect();
for h in handles { h.await.unwrap(); }
}).await;
}
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.
Threads 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() .scope_global(async move {
log::info!("Hello, world!");
}).await;
assert!(std::fs::read_to_string("/tmp/app.log").unwrap().ends_with("Hello, world!\n"));
}