Expand description
§mtlog-tokio
Scoped logging for tokio runtimes with support for log files.
§Usage
// 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;
}§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(); }
// logs are automatically flushed when scope_global completes
}).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() // disable stdout logging if needed
.scope_global(async move {
log::info!("Hello, world!");
// logs are automatically flushed when scope_global completes
}).await;
assert!(std::fs::read_to_string("/tmp/app.log").unwrap().ends_with("Hello, world!\n"));
}Structs§
- Config
Builder - Builder for configuring and initializing the logger.
- Size
Rotation Config - Configuration for size-based log file rotation.
- Time
Rotation Config - Configuration for time-based log file rotation.
Statics§
- LOG_
CONFIG - Thread-local logger configuration for finer control over logging settings per thread.
Functions§
- logger_
config - Returns a default ConfigBuilder for configuring the logger.