Skip to content

Latest commit

History

15 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

logcap

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 and crate.

Examples

Capture logs within a thread

use log::{info,Level};use logcap::assert_logs;#[test]fntest_logs(){
logcap::setup();// test logic outputting logsinfo!("foobar");info!("moocow");info!("hello, world!");// make assertions on logsassert_logs!("foobar","^m.*w$",(Level::Info,"hello, world!"));}

Capture logs across threads

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]fntest_logs(){
logcap::builder().scope(CaptureScope::Process).max_level(LevelFilter::Trace).setup();// test multi-threaded logic outputting logswarn!("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());});}

About

A Rust crate for capturing application log output

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages