Tracing: print in different timezone?

I am having difficulty finding info on how can I configure the timezone with which the timestamps emitted by tracing-log are printed.

Does anyone have a pointer?

If you're using tracing-log to capture log events in tracing I think this is the module in tracing-subscriber you want.

1 Like

Yes I am using that, thank you.

Would you mind me giving me an example if it's not too much trouble? I am reading the docs right now and will likely decipher them soon enough. For the moment I can't seem to figure out how to supply a non-UTC and non-local offset to feed to the with_timer method of Builder.

Hmm, let me try with time::UtcOffset.from_hms.

I'm currently (inadvisedly[1]) doing something like

tracing_subscriber::registry().with(
    fmt::layer()
        .with_timer(LocalTime::new(format_description!(
            "[year]-[month]-[day] [hour repr:24]:[minute]:[second]::[subsecond digits:4]"
        )))
        .with_span_events(FmtSpan::CLOSE)
        .with_writer(std::io::stdout),
);

in a project I had handy


  1. LocalOffset is cursed and you shouldn't use it, which is why it's behind a feature flag AND requires an additional opt in via RUSTFLAGS ↩︎

1 Like

Thank you! I ended up with:

    let offset = UtcOffset::from_hms(-8, 0, 0).expect("should get PST offset");
    let time_format = time::format_description::parse(
        "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:6]",
    )
    .expect("format string should be valid");
    let timer = OffsetTime::new(offset, time_format);

    let log_layer = tracing_subscriber::fmt::layer()
        .with_timer(timer)
        .compact()
        .with_thread_names(true)
        .with_filter(LevelFilter::TRACE);

    tracing_subscriber::registry()
        .with(log_layer)
        .init();

And it's working fine. Thanks again.

2 Likes

FYI, you can avoid the overhead of parsing the format description at runtime by using a macro. Just change time::format_description::parse to time::macros::format_description! and remove the .expect("…") call. You can similarly replace the UTC offset with time::macros::offset!(-8).

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.