d-e-s-o/test-log

Allow to set default log level

Closed this issue · 5 comments

Currently info output won't be displayed unless I set RUST_LOG=info.
I would suggest to add a default log level via Cargo feature.

That doesn't seem particularly flexible to me, because we'd have to hard code the default level to use, right? Meaning users wouldn't be able to select warn instead (unless we have yet another feature for that).

I think an argument could be made to always log at info by default, because stuff is captured anyway, but not sure whether that makes all that much sense to change.

Defaults would be very useful, sometimes coming back to the tests after a long time I forget I can run the tests with RUST_LOG=info.

One thing I've done in the past is checking to see if the RUST_LOG environment variable is not set before configuring env_logger to a default level. Are you interested in a PR with those changes? Also crate features so it's opt-in to a specific default.

Are you interested in a PR with those changes?

Can you elaborate some more on what exactly you had in mind?

EDIT: After some more thought, maybe this can be an attribute rather than a crate-wide feature that way it can be more configurable.

Maybe something like:

#[test_log::test(default_filter="info,foo=debug", tokio::test)]
async fn my_test() {

which expands to something like (I'm just making this up, not sure what the code expands to right now):

let _ = env_logger::builder().parse_env(Env::default().default_filter_or("info,foo=debug")).is_test(true).try_init();

Looking at the env_logger docs, this will parse the filter from RUST_LOG or default to "info,foo=debug" if there is nothing in the env:
https://docs.rs/env_logger/latest/env_logger/struct.Env.html#method.default_filter_or
https://docs.rs/env_logger/latest/src/env_logger/lib.rs.html#1080-1083

Haven't tested it though.

Ah, yeah, I think an argument to the attribute is a much easier sell than a crate-wide thing and would be entirely reasonable. Haven't thought through all the details yet either, but feel free to open a pull request.