Skip to main content

Crate mtlog

Crate mtlog 

Source
Expand description

§mtlog

Multi-threaded logger with support for progress bars and log files.

§Usage

// Cargo.toml
...
[dependencies]
mtlog = "0.2.0"
use mtlog::logger_config;

let _guard = logger_config()
   .init_global();
log::info!("Hello, world!");
// guard ensures logs are flushed when dropped

§Multi-threaded logging

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(); }
// guard ensures logs are flushed when dropped

§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::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"));

Structs§

ConfigBuilder
Builder for configuring and initializing the logger.
LoggerGuard
Guard that ensures the logger is properly shut down when dropped. Hold this guard for the lifetime of your logging session.
SizeRotationConfig
Configuration for size-based log file rotation.
TimeRotationConfig
Configuration for time-based log file rotation.

Functions§

logger_config
Returns a default ConfigBuilder for configuring the logger.