Crates.io | logcap |
lib.rs | logcap |
version | 0.1.3 |
created_at | 2025-09-02 18:43:05.714555+00 |
updated_at | 2025-09-14 19:43:40.491632+00 |
description | A library for capturing log output. |
homepage | https://github.com/kormide/logcap |
repository | https://github.com/kormide/logcap |
max_upload_size | |
id | 1821528 |
size | 30,680 |
A Rust crate for capturing application log output for integration testing.
The logger can have a threaded scope to capture logs from a thread, or with a process scope to capture across all threads.
See the documentation. Crate: https://crates.io/crates/logcap.
use log::{info, Level};
use logcap::assert_logs;
#[test]
fn test_logs() {
logcap::setup();
// test logic outputting logs
info!("foobar");
info!("moocow");
info!("hello, world!");
// make assertions on logs
assert_logs!(
"foobar",
"^m.*w$",
(Level::Info, "hello, world!")
);
}
Run tests with --test_threads=1
to avoid clobbering logs between tests, or use a mutex for synchronization.
use log::{LevelFilter,warn};
use logcap::{assert_logs, CaptureScope};
#[test]
fn test_logs() {
logcap::builder()
.scope(CaptureScope::Process)
.max_level(LevelFilter::Trace)
.setup();
// test multi-threaded logic outputting logs
warn!("warning from main thread");
let _ = thread::spawn(|| warn!("warning from thread 1")).join();
let _ = thread::spawn(|| warn!("warning from thread 2")).join();
// make assertions on logs
logcap::consume(|logs| {
assert_eq!(3, logs.len());
assert!(logs.iter.find(|log| log.body.contains("thread 1")).is_some());
assert!(logs.iter.find(|log| log.body.contains("thread 2")).is_some());
assert!(logs.iter.find(|log| log.body.contains("main thread")).is_some());
});
}