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

Timestamp support local timezone #158

Open
zonyitoo opened this issue Mar 24, 2020 · 8 comments
Open

Timestamp support local timezone #158

zonyitoo opened this issue Mar 24, 2020 · 8 comments

Comments

@zonyitoo
Copy link

Printing timestamp in local timezone is more useful than UTC.

@KodrAus
Copy link
Collaborator

KodrAus commented Jul 1, 2020

For output that's expected to be consumed by a person immediately it is definitely nicer to use the local timezone. For output that's picked up by a sidecar it's not necessarily better.

We could add a human_format() method to the Builder that's designed for consumption using a friendlier timestamp format and suitable for, say, diagnostic output from command-line tools. What do you think?

@zonyitoo
Copy link
Author

zonyitoo commented Jul 2, 2020

Yes, that's great.

@KodrAus KodrAus added this to the 0.7.x milestone Jul 19, 2020
@julianandrews
Copy link

I'd love to have this feature - my default use case for env_logger is lightweight CLI tools, and local time would be a big improvement.

I looked through the code, and it doesn't seem too hard to do - the main stumbling block I'm guessing is that humantime doesn't support timezone output.

@KodrAus - are you open to switching to chrono from humantime?

I threw together a really quick proof of concept, and it looks to me as if it would work well. chrono comes with functions for the same RFC3339 output at all the expected precisions. It's definitely a little heavier weight than humantime (minimal dependencies will be chrono, num-integer, and num-traits), but it's the de-facto standard Rust datetime library.

If this sounds good to you, I'd be happy to clean up my proof of concept and make it a PR.

@epage epage removed this from the 0.7.x milestone Nov 10, 2022
@AshfordN
Copy link

Any update on this feature? This is seems like a rather useful feature.

@philolo1
Copy link

If anybody else looks for this feature, its written here: https://rust-lang-nursery.github.io/rust-cookbook/development_tools/debugging/config_log.html#include-timestamp-in-log-messages:

use std::io::Write;
use chrono::Local;
use env_logger::Builder;
use log::LevelFilter;

fn main() {
    Builder::new()
        .format(|buf, record| {
            writeln!(buf,
                "{} [{}] - {}",
                Local::now().format("%Y-%m-%dT%H:%M:%S"),
                record.level(),
                record.args()
            )
        })
        .filter(None, LevelFilter::Info)
        .init();

    log::warn!("warn");
    log::info!("info");
    log::debug!("debug");
}

@DanceMore
Copy link

hm. as an operations / SRE type, I kinda wish this was an ENV setting to toggle UTC vs local.

I'm currently deploying some software where I'm using a Scheduler and I intended to use ENV TZ=$deploymentTimezone as a bit of a hack because the User Experience is expected to be local time, but all my container workers run UTC (even when they're physically in my local timezone ☁️ )

I currently have it deployed via docker compose with the TZ set but it's challenging to see if that config setting has "taken" because the debug/info logs are still coming out UTC.

as much as I am a fan of "all systems should use UTC", that's often an up-stack choice and sometimes the app depends on local time. also timezones can get weirder if you support Windows too. iirc, Windows will often write the hardware clock to local timezone and disregard UTC whereas UNIX-based systems tend to write the system clock to UTC and use ENV or locale in the display / presentation of the time.

@wez
Copy link

wez commented Dec 27, 2023

I would love for this to be integrated! FWIW, I think it would be reasonable to make this a crate feature that only pulls in chrono for the local time formatting, which would allow folks to opt-in or opt-out of the additional weight of chrono.

Meanwhile, for those that want to match the default colorized style, but with local time:

.format(|buf, record| {
    use env_logger::fmt::Color;
    use std::io::Write;

    let subtle = buf
        .style()
        .set_color(Color::Black)
        .set_intense(true)
        .clone();
    write!(buf, "{}", subtle.value("["))?;
    write!(buf, "{} ", chrono::Local::now().format("%Y-%m-%dT%H:%M:%S"))?;
    write!(buf, "{:<5}", buf.default_styled_level(record.level()))?;
    if let Some(path) = record.module_path() {
        write!(buf, " {}", path)?;
    }
    write!(buf, "{}", subtle.value("]"))?;
    writeln!(buf, " {}", record.args())
})

@kelvie
Copy link

kelvie commented Sep 28, 2024

For those of us in the future where the previous comment doesn't work, here's a version that uses the new included anstyle package (didn't include the module path)

        .format(|buf, record| {
            use std::io::Write;
            use chrono::Local;
            use env_logger::fmt::style::{Style, AnsiColor};

            let subtle = Style::new()
                .fg_color(Some(AnsiColor::BrightBlack.into()));
            let level_style = buf.default_level_style(record.level());

            writeln!(buf,
                     "{subtle}[{subtle:#}{} {level_style}{:<5}{level_style:#}{subtle}]{subtle:#} {}",
                     Local::now().format("%Y-%m-%d %H:%M:%S%Z"),
                     record.level(),
                     record.args()
            )
        })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants