pub fn init_log_capture(
max_level: impl Into<LevelFilter>,
print_logs: bool,
) -> (LogCapture, impl Subscriber + Send + Sync)Expand description
Initialises a log capture utility for testing, with optional log printing.
This function sets up a LogCapture instance to capture logs during test execution.
It also configures a tracing_subscriber with the specified maximum log level
and a writer that directs logs to LogCapture. If print_logs is enabled, logs
up to max_level are also printed to the test output.
§Arguments
max_level- The maximum log level to capture and print, which can be converted intoLevelFilter.print_logs- Iftrue, logs up tomax_levelwill also be printed to the test output.
§Returns
A tuple containing:
LogCapture: The log capture instance.Subscriber: A configuredtracing_subscriberthat captures logs.
§Examples
use sp_tracing::{
test_log_capture::init_log_capture,
tracing::{info, subscriber, Level},
};
let (log_capture, subscriber) = init_log_capture(Level::INFO, false);
subscriber::with_default(subscriber, || {
info!("This log will be captured");
assert!(log_capture.contains("This log will be captured"));
});§Usage Guide
- If you only need to capture logs for assertions without printing them, use
init_log_capture(max_level, false). - If you need both capturing and printing logs, use
init_log_capture(max_level, true). - If you only need to print logs but not capture them, use
sp_tracing::init_for_tests().