Skip to content

subscriber: default timestamp formatter without chrono is SystemTime's Debug impl #791

Closed
@hawkw

Description

@hawkw

Bug Report

Version

tracing-subscriber 0.2.7

Crates

tracing-subscriber

Description

By default, tracing-subscriber's fmt module uses the chrono crate for formatting timestamps. However, the chrono dependency is optional and can be disabled independently of fmt. This is correct --- users who are using the Uptime timestamp formatter, providing their own custom timestamp formatter, or not using timestamps don't need the chrono dependency and may want to disable it.

However, when the chrono feature flag is disabled, the default formatter still enables SystemTime timestamps by default:

impl Default for Format<Full, SystemTime> {
fn default() -> Self {
Format {
format: Full,
timer: SystemTime,
ansi: true,
display_target: true,
display_level: true,
}
}
}

So, the formatter will still emit timestamps unless without_time(true) is called.

What do we do when timestamps are enabled, but we can't use chrono to format them because the dependency is not enabled?

It turns out that we fall back to SystemTime's fmt::Debug impl:

#[cfg(not(feature = "chrono"))]
impl FormatTime for SystemTime {
fn format_time(&self, w: &mut dyn fmt::Write) -> fmt::Result {
write!(w, "{:?}", std::time::SystemTime::now())
}
}

The formatted output from this is...not great:
Screenshot_20200709_092349

I do think it's correct to rely on chrono (or arbitrary user-defined timestamp formatters) for complex human-readable formatting. But the fmt::Debug implementation is not ideal. It includes the struct's name, field names, and curly braces, which add a lot of noise and make the log lines much longer. It leaks the SystemTime type's internal layout, which may be different depending on OS. And, the width of the numeric fields are not padded to a fixed width, so the log lines are not aligned.

We should try to fall back to something a little nicer when chrono isn't present. It doesn't need to be fancy --- we can (and should!) rely on chrono for human readable formats. Maybe just the number of seconds since the Unix epoch or something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    crate/subscriberRelated to the `tracing-subscriber` crategood first issueGood for newcomerskind/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions