emabee / flexi_logger

A flexible logger for rust programs that can write to stderr, stdout, and/or to log files
Apache License 2.0
315 stars 55 forks source link

use_utc() in v0.24.0 doesn't seem to work anymore #123

Closed tdudz closed 2 years ago

tdudz commented 2 years ago

i updated my stack to 0.24.0 and even though i set use_utc() when making the Logger handle, it still prints out using my local time

i create the logger using the following:

Logger::try_with_str("info")
        .unwrap()
        .log_to_stdout()
        .write_mode(WriteMode::Async)
        .format(
            |w: &mut dyn std::io::Write,
             now: &mut flexi_logger::DeferredNow,
             record: &log::Record| {
                write!(
                    w,
                    "{} [{}] {}",
                    now.now().format("%Y-%m-%d %H:%M:%S%.6f"),
                    record.level(),
                    &record.args()
                )
            },
        )
        .use_utc()
        .start()
        .unwrap()

i tested it by reverting to the previous flexi_logger version and the timestamps were UTC again. i suspect it might have something to do with the removal of time and re-addition of chrono

emabee commented 2 years ago

use_utc() is now implemented in DeferredNow::format() , while the rest of DeferredNow only uses local time. Especially DeferredNow::now() returns DateTime<Local>.

If you change your code snippet from

    now.now().format("%Y-%m-%d %H:%M:%S%.6f"),

to

    now.format("%Y-%m-%d %H:%M:%S%.6f"),

then it works as intended.

The behavior is a bit unclear, though. I need to improve the docu. And more: I just saw that DeferredNow::format_rfc3164() is not going through format() and thus ignores use_utc(), which is of course a real bug.

tdudz commented 2 years ago

thanks, that worked!

emabee commented 2 years ago

Version 0.24.1 fixes also the bug in deferredNow::format_rfc3164().