base: log with local timestamps instead of UTC

00068bc0a2f02bd79235ff50b9d6f81a6133986f caused the timestamps on
crosvm logs to switch to UTC (ISO 8601). The 8601 part is great,
but the UTC time is a little annoying for local debugging.

Thanks to auradkar@ for coming up with the new formatter code.

BUG=b:236004673
TEST=ran downstream crosvm & verified logs appear w/ local timestamps.

Change-Id: I715f68b7f4ba545e3eadbfd1b9be9abba69d8258
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3712544
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Vikram Auradkar <auradkar@google.com>
Commit-Queue: Noah Gold <nkgold@google.com>
This commit is contained in:
Noah Gold 2022-06-17 17:50:32 -07:00 committed by Chromeos LUCI
parent 918bdde54f
commit 972ed6d094

View file

@ -49,8 +49,9 @@
//!
//! [log-crate-url]: https://docs.rs/log/
use std::{fmt::Display, io};
use std::{fmt::Display, io, io::Write};
use chrono::Local;
use once_cell::sync::OnceCell;
use remain::sorted;
use serde::{Deserialize, Serialize};
@ -247,8 +248,25 @@ impl State {
builder.parse(cfg.filter);
let filter = builder.build();
if cfg.stderr {
let create_formatted_builder = || {
let mut builder = env_logger::Builder::new();
// Output log lines w/ local ISO 8601 timestamps.
builder.format(|buf, record| {
writeln!(
buf,
"[{} {:5} {}] {}",
Local::now().format("%Y-%m-%dT%H:%M:%S%:z"),
record.level(),
record.module_path().unwrap_or("<missing module path>"),
record.args()
)
});
builder
};
if cfg.stderr {
let mut builder = create_formatted_builder();
builder.filter_level(log::LevelFilter::Trace);
builder.target(env_logger::Target::Stderr);
loggers.push(Box::new(builder.build()));
@ -259,7 +277,7 @@ impl State {
}
if let Some(file) = cfg.pipe {
let mut builder = env_logger::Builder::new();
let mut builder = create_formatted_builder();
builder.filter_level(log::LevelFilter::Trace);
builder.target(env_logger::Target::Pipe(Box::new(file)));
// https://github.com/env-logger-rs/env_logger/issues/208