Module log_testing

Source
Expand description

Requires the log_testing feature. Utilities for log testing, i.e. tests which ensure the log output is correct.

These are most useful to developers of log4rs extension libraries, although any application could choose to test its log output. Unless you fall into that narrow camp, you probably want test_logging.

use log::{error, info};
use log4rs_test_utils::log_testing::logging_test_setup_mock;

#[test]
fn simple_mock_example() {
    let (_guard, logs_handle) = logging_test_setup_mock(None, None);

    info!("Hello, world!");
    error!("Oh, no!");
    info!("Goodbye, world.");

    let logs = logs_handle.lock().unwrap();
    assert_eq!(logs.len(), 3);
    assert_eq!(logs.iter().filter(|s| s.contains("INFO")).count(), 2);
    assert_eq!(logs.iter().filter(|s| s.contains("ERROR")).count(), 1);
    assert_eq!(logs.iter().filter(|s| s.contains(", world")).count(), 2);
}

Structs§

MockAppender
A mock appender that encodes its messages to a Vec<String>.

Functions§

logging_test_setup
Call this at the start of a logging test to configure the logger.
logging_test_setup_mock
A convenient wrapper for logging_test_setup that configures the global logger with a fresh MockAppender.

Type Aliases§

LogsHandle
A thread-safe handle to the list of log messages written by a MockAppender.