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

EnvFilter overwrites all attempts to set a default value #1466

Open
tv42 opened this issue Jul 13, 2021 · 5 comments
Open

EnvFilter overwrites all attempts to set a default value #1466

tv42 opened this issue Jul 13, 2021 · 5 comments

Comments

@tv42
Copy link

tv42 commented Jul 13, 2021

Bug Report

Version

├── tracing v0.1.26
│   ├── tracing-attributes v0.1.15 (proc-macro)
│   └── tracing-core v0.1.18
├── tracing-subscriber v0.2.19
│   ├── tracing v0.1.26 (*)
│   ├── tracing-core v0.1.18 (*)
│   ├── tracing-log v0.1.2
│   │   └── tracing-core v0.1.18 (*)
│   └── tracing-serde v0.1.2
│       └── tracing-core v0.1.18 (*)

Platform

Linux 5.4.109 x86_64

Description

EnvFilter has no way of setting defaults.

.add_directive is wrong (and the examples lead to bad behavior) because it overrides $RUST_LOG, as it is added to the end of whatever the user specified, overriding e.g. RUST_LOG=trace.

SubscriberBuilder::with_max_level is wrong because .with_env_filter explicit overwrites that, as per documentation.

EnvFilter internally defaults to ERROR (not documented anywhere).

There is no clean way to achieve

  1. Default level INFO
  2. User can override that with RUST_LOG=warn

This is related to #1400 but I don't think the fallback value necessarily solves the problem (consider desired application default level INFO and RUST_LOG=some_crate::error) and I don't want the problem be marked closed because a proposed solution is rejected.

@tv42
Copy link
Author

tv42 commented Jul 13, 2021

Here's the ugly workaround:

const DEFAULT_LEVEL: tracing::Level = tracing::Level::INFO;
let input = std::env::var("RUST_LOG").expect("fixme");
let mut directives = DEFAULT_LEVEL.to_string();
if !input.is_empty() {
    directives.push(',');
    directives.push_str(&input);
}
let filter = tracing_subscriber::EnvFilter::try_new(directives).expect("fixme");
tracing_subscriber::fmt()
    .with_env_filter(filter)
    .init();

@Imberflur
Copy link

A feature that solves this would be quite useful. We have a similar workaround: https://gitlab.com/veloren/veloren/-/blob/cba946c07b5ec42fb5c47b1c59f5bc8def40e837/common/frontend/src/lib.rs#L87

@vilgotf
Copy link

vilgotf commented Dec 16, 2021

This is already supported, just needs to be better documented.

tracing_subscriber::fmt()
	.with_env_filter(
		EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
	)
	.init();

@Imberflur
Copy link

This is already supported, just needs to be better documented.

tracing_subscriber::fmt()
	.with_env_filter(
		EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
	)
	.init();

@vilgotf I'm looking back over this and I think it doesn't address my specific use case of having several directives that are applied before the ones from RUST_LOG

@Imberflur
Copy link

Imberflur commented Mar 13, 2022

Thinking about this further, I think it could be convenient to have a method to parse and apply directives from an env var to an existing EnvFilter instance (with errors returned if something fails to parse or the env var is missing or not unicode).

Another idea that came to mind is an add_directive_if_missing/add_directive_no_overwrite method.

bors bot added a commit to get10101/itchysats that referenced this issue Jun 29, 2022
2321: Workaround to prevent overwriting RUST_LOG r=klochowicz a=bonomat

So far we overwrote RUST_LOG log settings using . This is annoying because hardcoded log levels can't be changed later on. With this workaround we can have smart defaults but allow the user to overwrite the log level using RUST_LOG

// edit// unfortunately tracing does not allow us to do this differently. There is an open ticket which inspired this solution: tokio-rs/tracing#1466

Co-authored-by: Philipp Hoenisch <philipp@hoenisch.at>
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

3 participants