Skip to content

Targets::with_targets ignores default #1790

Closed
@recmo

Description

@recmo

Bug Report

Version

`cargo tree | grep tracing`
│   │   ├── tracing-core v0.1.21
│   │   └── tracing-error v0.1.2
│   │       ├── tracing v0.1.29
│   │       │   ├── tracing-attributes v0.1.18 (proc-macro)
│   │       │   └── tracing-core v0.1.21 (*)
│   │       └── tracing-subscriber v0.2.25
│   │           └── tracing-core v0.1.21 (*)
│   └── tracing-error v0.1.2 (*)
│   │   │   │   │   └── tracing v0.1.29 (*)
│   │   │   │   └── tracing v0.1.29 (*)
│   │   │   │   ├── tracing v0.1.29 (*)
│   │   │   │   └── tracing v0.1.29 (*)
│   │   │   ├── tracing v0.1.29 (*)
│   │   │   └── tracing-futures v0.2.5
│   │   │       └── tracing v0.1.29 (*)
│   │   └── tracing-core v0.1.21 (*)
│   ├── tracing v0.1.29 (*)
│   ├── tracing-core v0.1.21 (*)
│   └── tracing-subscriber v0.3.3
│       ├── tracing v0.1.29 (*)
│       ├── tracing-core v0.1.21 (*)
│       ├── tracing-log v0.1.2
│       │   └── tracing-core v0.1.21 (*)
│       └── tracing-serde v0.1.2
│           └── tracing-core v0.1.21 (*)
│   │   │   │   │   │   ├── tracing v0.1.29
│   │   │   │   │   │   │   └── tracing-core v0.1.21
│   │   │   ├── tracing v0.1.29 (*)
│   │   │   ├── tracing-futures v0.2.5 (*)
│   │   ├── tracing v0.1.29 (*)
│   │   ├── tracing-futures v0.2.5 (*)
│       ├── tracing v0.1.29 (*)
│       ├── tracing v0.1.29 (*)
│       └── tracing-futures v0.2.5 (*)
├── tracing v0.1.29 (*)
├── tracing-futures v0.2.5 (*)
├── tracing-opentelemetry v0.16.0
│   ├── tracing v0.1.29 (*)
│   ├── tracing-core v0.1.21 (*)
│   ├── tracing-log v0.1.2 (*)
│   └── tracing-subscriber v0.3.3 (*)
├── tracing-subscriber v0.3.3 (*)
├── tracing-test v0.2.1
│   ├── tracing-core v0.1.21 (*)
│   ├── tracing-subscriber v0.3.3 (*)
│   └── tracing-test-macro v0.2.1 (proc-macro)

Platform

Darwin MacBook-Pro-16-inch-2021-2.local 21.2.0 Darwin Kernel Version 21.2.0: Sun Nov 28 20:28:41 PST 2021; root:xnu-8019.61.5~1/RELEASE_ARM64_T6000 arm64

Crates

tracing_subscriber

Description

Merging two Targets objects using first.with_targets(second) ignores the default value set in second.

Example

let first = Targets::new()
    .with_default(Level::INFO)
    .with_target("app", Level::DEBUG);
let second = Targets::new()
    .with_default(Level::DEBUG)
    .with_target("app", Level::TRACE);
let combined = first.with_targets(second);
dbg!(&combined);

results in

[src/cli/logging.rs:56] &combined = Targets(
    DirectiveSet {
        directives: [
            StaticDirective {
                target: Some(
                    "app",
                ),
                field_names: [],
                level: LevelFilter::TRACE,
            },
            StaticDirective {
                target: None,
                field_names: [],
                level: LevelFilter::INFO,
            },
        ],
        max_level: LevelFilter::TRACE,
    },
)

where the app directive is correctly overridden by second, but the original default directive is retained.

In fact, there appears to be no method to access the default level on a Targets, so I can not even write a workaround.

I'm using this pattern to combine the --verbose CLI argument (first) with a --log-filter argument (second). I want the log-filter to take precedence for all directives, including default.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions