Proper way to use simplelog in #[test] code

⚓ Rust    📅 2025-11-23    👤 surdeus    👁️ 11      

surdeus

I have code that uses logger extensively. I'd like my #[test] sections to send their logger output to standard output, where it would be hidden unless the test fails.
There are some problems. First, if a test does an init of a CombinedLogger, that clashes with other tests in the same test run. There's only one test executable. We can get around that by ignoring the output from init(). Second, if I do this:

let _ = simplelog::CombinedLogger::init(
        vec![
            simplelog::TermLogger::new(LevelFilter::Debug, simplelog::Config::default(), simplelog::TerminalMode::Stdout, simplelog::ColorChoice::Auto),]
    );

it really logs to the terminal, not standard output, so all the logger output shows up rather than being captured for successful tests.

There's probably some simple way to do this.

2 posts - 2 participants

Read full topic

🏷️ Rust_feed