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

subscriber: added explicit with_timestamp method for any impls that use without_time #2084

Closed

Conversation

mkatychev
Copy link

@mkatychev mkatychev commented Apr 20, 2022

Motivation

  • Using .without_time() is inconsistent and lacks the ergonimics of current Builder::with_flag methods.

Example of how timestamps are currently toggled:

impl Opts {
    pub fn init(&self) -> Result<(), SetGlobalDefaultError> {
        let filter = EnvFilter::builder()
            .with_env_var("RUST_LOG")
            .with_default_directive(LevelFilter::DEBUG.into())
            .from_env_lossy();
        // Initialize tracing;
        let collector = fmt::Subscriber::builder()
            .with_env_filter(filter)
            .with_line_number(self.line_number)
            .with_file(self.file)
            .with_thread_ids(self.thread_ids)
            .with_target(self.target)
            .with_ansi(self.ansi);
        // life becomes difficult from here on
        if !self.timestamp {
            return tracing::subscriber::set_global_default(collector.without_time().finish());
        };
        tracing::subscriber::set_global_default(collector.finish())
    }
}

Solution

  • Added tracing_subscriber::fmt::Layer::with_timestamp(self, display_timestamp: bool)
  • Changed timer type to an Option:
pub struct Format<F = Full, T = SystemTime> {
    // ..
-   pub(crate) timer: <T>,
+   pub(crate) timer: Option<T>,
    // ..
}
  • Retained all without_time() methods to maintain backwards compatibility

@mkatychev mkatychev requested review from hawkw, davidbarsky and a team as code owners April 20, 2022 22:42
/// Do not emit timestamps with log messages.
pub fn without_time(self) -> CollectorBuilder<N, format::Format<L, ()>, F, W> {
pub fn without_time(self) -> CollectorBuilder<N, format::Format<L, T>, F, W> {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should without_time be marked with a deprecated flag?

@mkatychev
Copy link
Author

Do we want to maintain impl FormatTime for () or should it have a deprecation flag now that it is replaced by Option::None as <T: FormatTime>?

impl FormatTime for () {
fn format_time(&self, _: &mut Writer<'_>) -> fmt::Result {
Ok(())
}
}

@mkatychev mkatychev requested a review from davidbarsky April 20, 2022 23:35
@hawkw
Copy link
Member

hawkw commented Apr 22, 2022

Do we want to maintain impl FormatTime for () or should it have a deprecation flag now that it is replaced by Option::None as <T: FormatTime>?

impl FormatTime for () {
fn format_time(&self, _: &mut Writer<'_>) -> fmt::Result {
Ok(())
}
}

The implementation for () has less runtime overhead than the implementation for Option, as it doesn't have to check at runtime whether the Option is Some or None; it just always does nothing. Therefore, I don't think it should be removed, as it's preferable in cases where the timestamp formatter will never be enabled.

@boardwalk
Copy link

I ran into this recently (no with_timestamp method, and without_time changing the type making things a little more ugly).

The other, related thing, I ran into is not being able to toggle display_timestamp off separately from time as a whole, which, from my shallow understanding of the code, would stop the timestamp from being displayed (which is what I want, I have an outside process that timestamps logs) but not the duration timing (which I want to keep). It seems to be an all or nothing thing.

What if there was a with_timestamp that worked like the other with_* functions that just change a display_* bool? Calling something that disables durations as well as the timestamps with_timestamp as in this PR seems wrong.

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 this pull request may close these issues.

4 participants