mtlog-tokio

Crates.iomtlog-tokio
lib.rsmtlog-tokio
version0.2.1
created_at2024-10-16 10:25:45.315658+00
updated_at2026-01-03 18:59:54.210779+00
descriptionScoped logging for tokio runtimes with support for log files.
homepagehttps://github.com/AntoineRenaud91/mtlog-rs
repositoryhttps://github.com/AntoineRenaud91/mtlog-rs
max_upload_size
id1411628
size35,416
Tonio Rd (AntoineRenaud91)

documentation

https://docs.rs/mtlog

README

mtlog-tokio

Crates.io Documentation

Scoped logging for tokio runtimes with per-task configuration and support for log files.

Related Crates

  • mtlog - Use this instead for non-async, standard multi-threaded applications
  • mtlog-progress - Add this for progress bars that work gracefully with mtlog

Installation

Add this to your Cargo.toml:

[dependencies]
mtlog-tokio = "0.2"
tokio = { version = "1", features = ["full"] }

Features

  • Scoped logging with automatic cleanup using async/await
  • Task-local logger configuration
  • File logging with automatic file creation and appending
  • Configurable log levels and output destinations
  • Designed specifically for tokio async runtimes

Usage

Basic Usage

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-task 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 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;
}

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. 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"));
}

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

Documentation

For detailed API documentation, visit docs.rs/mtlog-tokio.

License

MIT

Commit count: 12

cargo fmt