Crate testing_logger[][src]

This crate supports testing and asserting that appropriate log messages from the log crate are generated during tests.

Log events are captured in a thread_local variable so this module behaves correctly when tests are run multithreaded.

All log levels are captured, but none are sent to any logging system. The test developer should use the validate() function in order to check the captured log messages.

Examples

#[macro_use]
extern crate log;
use log::Level;
extern crate testing_logger;

#[test]
fn test_something() {
    testing_logger::setup();
    warn!("Something went wrong with {}", 10);
    testing_logger::validate( |captured_logs| {
        assert_eq!(captured_logs.len(), 1);
        assert_eq!(captured_logs[0].body, "Something went wrong with 10");
        assert_eq!(captured_logs[0].level, Level::Warn);
    });
}

The target is also captured if you want to validate that.


#[test]
fn test_target() {
    testing_logger::setup();
    log!(target: "documentation", Level::Trace, "targetted log message");
    testing_logger::validate( |captured_logs| {
        assert_eq!(captured_logs.len(), 1);
        assert_eq!(captured_logs[0].target, "documentation");
        assert_eq!(captured_logs[0].body, "targetted log message");
        assert_eq!(captured_logs[0].level, Level::Trace);
    });
}

Structs

CapturedLog

A captured call to the logging system. A Vec of these is passed to the closure supplied to the validate() function.

Functions

setup

Prepare the testing_logger to capture log messages for a test.

validate

Used to validate any captured log events.