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

Allow to set default log level #25

Closed
Shatur opened this issue Mar 1, 2023 · 5 comments
Closed

Allow to set default log level #25

Shatur opened this issue Mar 1, 2023 · 5 comments
Assignees

Comments

@Shatur
Copy link

Shatur commented Mar 1, 2023

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.

@d-e-s-o
Copy link
Owner

d-e-s-o commented Mar 2, 2023

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.

@DarrenTsung
Copy link
Contributor

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.

@d-e-s-o
Copy link
Owner

d-e-s-o commented Oct 5, 2023

Are you interested in a PR with those changes?

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

@DarrenTsung
Copy link
Contributor

DarrenTsung commented Oct 5, 2023

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.

@d-e-s-o
Copy link
Owner

d-e-s-o commented Oct 5, 2023

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.

DarrenTsung added a commit to DarrenTsung/test-log that referenced this issue Oct 23, 2023
Users can now specify a default_log_filter via #[test_log(default_log_filter = "foo")]
which will be used when RUST_LOG is not specified.

Please note that because env_logger is initialized globally, it is
possible that this value will be ignored if the logger is already
initialized.

Fixes: d-e-s-o#25
DarrenTsung added a commit to DarrenTsung/test-log that referenced this issue Oct 30, 2023
Users can now specify a default_log_filter via #[test_log(default_log_filter = "foo")]
which will be used when RUST_LOG is not specified.

Please note that because env_logger is initialized globally, it is
possible that this value will be ignored if the logger is already
initialized.

Fixes: d-e-s-o#25
DarrenTsung added a commit to DarrenTsung/test-log that referenced this issue Oct 30, 2023
Users can now specify a default_log_filter via #[test_log(default_log_filter = "foo")]
which will be used when RUST_LOG is not specified.

Please note that because env_logger is initialized globally, it is
possible that this value will be ignored if the logger is already
initialized.

Fixes: d-e-s-o#25
DarrenTsung added a commit to DarrenTsung/test-log that referenced this issue Oct 30, 2023
Users can now specify a default_log_filter via #[test_log(default_log_filter = "foo")]
which will be used when RUST_LOG is not specified.

Please note that because env_logger / tracing is initialized globally,
it is possible that this value will be ignored if they have already been
initialized by a different test.

Fixes: d-e-s-o#25
d-e-s-o pushed a commit that referenced this issue Nov 4, 2023
Users can now specify a default_log_filter via #[test_log(default_log_filter = "foo")]
which will be used when RUST_LOG is not specified.

Please note that because env_logger / tracing is initialized globally,
it is possible that this value will be ignored if they have already been
initialized by a different test.

Fixes: #25
d-e-s-o pushed a commit that referenced this issue Nov 4, 2023
Users can now specify a default_log_filter via #[test_log(default_log_filter = "foo")]
which will be used when RUST_LOG is not specified.

Please note that because env_logger / tracing is initialized globally,
it is possible that this value will be ignored if they have already been
initialized by a different test.

Fixes: #25
d-e-s-o pushed a commit that referenced this issue Nov 4, 2023
Users can now specify a default_log_filter via #[test_log(default_log_filter = "foo")]
which will be used when RUST_LOG is not specified.

Please note that because env_logger / tracing is initialized globally,
it is possible that this value will be ignored if they have already been
initialized by a different test.

Fixes: #25
@d-e-s-o d-e-s-o closed this as completed in 858ad05 Nov 4, 2023
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

Successfully merging a pull request may close this issue.

3 participants