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

Format::without_time makes it hard to enable/disable timestamps at runtime #3180

Open
azerupi opened this issue Dec 30, 2024 · 0 comments
Open

Comments

@azerupi
Copy link

azerupi commented Dec 30, 2024

Bug Report

Version

tracing = "0.1.41"
tracing-subscriber = "0.3.19"

But this is also present in master.

Crates

  • tracing-subscriber

Description

I'm trying to display the timestamp depending on the log level but the API makes this harder than expected.

In the Format methods there are a bunch of methods of the form with_xxx(self, bool). But the method that allows enabling or disabling the timestamps breaks this pattern by being called without_time(self) and not accepting a boolean.

Additionally, calling without_time changes the type, so its not as simple as just breaking up the builder to add an if.

https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/format/struct.Format.html#method.without_time

Desired:

let format = fmt::format()
    .with_level(true)
    .with_target(show_log_details)
    .with_thread_ids(false)
    .with_thread_names(false)
    .with_timestamp(show_log_details)
    .compact();

tracing_subscriber::fmt()
    .event_format(format)
    .with_max_level(verbosity)
    .init();

Current solution:

let show_log_details = matches!(verbosity, Verbosity::Debug | Verbosity::Trace);

let format = fmt::format()
    .with_level(true)
    .with_target(show_log_details)
    .with_thread_ids(false)
    .with_thread_names(false)
    .compact();

if show_log_details {
    tracing_subscriber::fmt()
        .event_format(format)
        .with_max_level(verbosity)
        .init();
} else {
    let format = format.without_time();

    tracing_subscriber::fmt()
        .event_format(format)
        .with_max_level(verbosity)
        .init();
}

I saw that there was an open PR to fix this: #2084 but it hasn't received any updates since 2022.

@azerupi azerupi changed the title Format::without_time seems to break the pattern by not accepting a boolean Format::without_time makes it hard to enable/disable timestamps at runtime Dec 30, 2024
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

1 participant