diff --git a/base/src/syslog.rs b/base/src/syslog.rs index 4a4f2e2b07..145e00f8ba 100644 --- a/base/src/syslog.rs +++ b/base/src/syslog.rs @@ -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(""), + 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