5 releases
0.1.4 | Oct 16, 2024 |
---|---|
0.1.3 | Oct 14, 2024 |
0.1.2 | Oct 14, 2024 |
0.1.1 | Oct 14, 2024 |
0.1.0 | Oct 14, 2024 |
#665 in Concurrency
51 downloads per month
16KB
308 lines
mtlog
Multi-threaded logger with support for progress bars and log files.
Usage
// Cargo.toml
...
[dependencies]
mtlog = "0.1.4"
use mtlog::logger_config;
logger_config()
.init_global();
log::info!("Hello, world!");
std::thread::sleep(std::time::Duration::from_millis(1)); // wait for log to flush
Multi-threaded logging
use mtlog::logger_config;
logger_config()
.with_name("main")
.init_global();
log::info!("Hello, world from main thread!");
for i in 0..5 {
std::thread::spawn(move || {
logger_config()
.with_name(&format!("thread {i}"))
.init_local();
log::warn!("Hello, world from thread {i}!")
});
}
std::thread::sleep(std::time::Duration::from_millis(1)); // wait for log to flush
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;
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!");
std::thread::sleep(std::time::Duration::from_millis(1)); // wait for log to flush
assert!(std::fs::read_to_string("/tmp/app.log").unwrap().ends_with("Hello, world!\n"));
Dependencies
~1–11MB
~66K SLoC