Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture logs inside tests? #54

Open
sourcefrog opened this issue Apr 26, 2020 · 2 comments
Open

Capture logs inside tests? #54

sourcefrog opened this issue Apr 26, 2020 · 2 comments

Comments

@sourcefrog
Copy link
Contributor

Hi,

It would be great if tests could be written to the tests's stdout, and then (by default) shown only if the test fails.

It looks like .chain(std::io::stdout()) sends them to the process's real stdout, bypassing the test framework's hooks. I see there's a lot of history on this in rust-lang/rust#42474 and perhaps it's not possible for Fern to fix? But it seems like if println! is captured, at least Fern could simply do println! for everything.

Or perhaps this is possible already and I'm just missing it, and if so perhaps it could be clearer in the docs?

@sourcefrog
Copy link
Contributor Author

It seems something like this will work. If there's no simpler way could I suggest this be added to the examples?

fn install_test_logger() {
    // This'll fail if called twice; don't worry.
    let _ = fern::Dispatch::new()
        // ...
        .level(log::LevelFilter::Debug)
        .chain(fern::Output::call(|record| println!("{}", record.args())))
        .apply();
}```

@daboross
Copy link
Owner

Thanks! I hadn't realized that the test framework's hook was per-thread rather than global. Makes sense now that I think about it, though.

Adding that as an example sounds like a good idea. It might also be worth adding to this note as well (or we could just remove the stdout from this example; it isn't really relevant):

fern/src/builders.rs

Lines 691 to 701 in 6666bd2

/// fern::Dispatch::new()
/// // format, etc.
/// .chain(std::io::stdout())
/// .chain(
/// fern::Dispatch::new()
/// .level(log::LevelFilter::Error)
/// .chain(fern::Panic),
/// )
/// # /*
/// .apply()?;
/// # */ .into_log();
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants