referrerpolicy=no-referrer-when-downgrade

Macro capture_test_logs

Source
macro_rules! capture_test_logs {
    ($max_level:expr, $print_logs:expr, $test:block) => { ... };
    ($max_level:expr, $test:block) => { ... };
    ($test:block) => { ... };
}
Expand description

Macro for capturing logs during test execution.

This macro sets up a log subscriber with a specified maximum log level and an option to print logs to the test output while capturing them.

§Arguments

  • $max_level: The maximum log level to capture.
  • $print_logs: Whether to also print logs to the test output.
  • $test: The block of code where logs are captured.

§Examples

use sp_tracing::{
    capture_test_logs,
    tracing::{info, warn, Level},
};

// Capture logs at WARN level without printing them
let log_capture = capture_test_logs!(Level::WARN, false, {
    info!("Captured info message");
    warn!("Captured warning");
});

assert!(!log_capture.contains("Captured info message"));
assert!(log_capture.contains("Captured warning"));

// Capture logs at TRACE level and also print them
let log_capture = capture_test_logs!(Level::TRACE, true, {
    info!("This will be captured and printed");
});

assert!(log_capture.contains("This will be captured and printed"));
  • init_log_capture(): Captures logs for assertions.
  • sp_tracing::init_for_tests(): Outputs logs but does not capture them.