diff --git a/tracing-appender/Cargo.toml b/tracing-appender/Cargo.toml index 3a0882dd2..33f8f36f4 100644 --- a/tracing-appender/Cargo.toml +++ b/tracing-appender/Cargo.toml @@ -20,6 +20,10 @@ keywords = ["logging", "tracing", "file-appender", "non-blocking-writer"] edition = "2018" rust-version = "1.63.0" +[features] +default = [] +local-time = ["time/local-offset"] + [dependencies] crossbeam-channel = "0.5.6" time = { version = "0.3.2", default-features = false, features = ["formatting", "parsing"] } diff --git a/tracing-appender/src/rolling.rs b/tracing-appender/src/rolling.rs index c98e9d58b..3c93d7bf1 100644 --- a/tracing-appender/src/rolling.rs +++ b/tracing-appender/src/rolling.rs @@ -192,7 +192,7 @@ impl RollingFileAppender { ref max_files, } = builder; let directory = directory.as_ref().to_path_buf(); - let now = OffsetDateTime::now_utc(); + let now = Self::now_local_or_utc(); let (state, writer) = Inner::new( now, rotation.clone(), @@ -215,7 +215,16 @@ impl RollingFileAppender { return (self.now)(); #[cfg(not(test))] - OffsetDateTime::now_utc() + Self::now_local_or_utc() + } + + #[inline] + fn now_local_or_utc() -> OffsetDateTime { + #[cfg(not(feature = "local-time"))] + return OffsetDateTime::now_utc(); + + #[cfg(feature = "local-time")] + OffsetDateTime::now_local().expect("Invalid localtime; this is a bug in tracing-appender") } } @@ -554,13 +563,32 @@ impl Rotation { } fn date_format(&self) -> Vec> { - match *self { + #[cfg(not(feature = "local-time"))] + return match *self { Rotation::MINUTELY => format_description::parse("[year]-[month]-[day]-[hour]-[minute]"), Rotation::HOURLY => format_description::parse("[year]-[month]-[day]-[hour]"), Rotation::DAILY => format_description::parse("[year]-[month]-[day]"), Rotation::WEEKLY => format_description::parse("[year]-[month]-[day]"), Rotation::NEVER => format_description::parse("[year]-[month]-[day]"), } + .expect("Unable to create a formatter; this is a bug in tracing-appender"); + + #[cfg(feature = "local-time")] + match *self { + Rotation::MINUTELY => format_description::parse( + "[year]-[month]-[day]-[hour]-[minute][offset_hour sign:mandatory][offset_minute]", + ), + Rotation::HOURLY => format_description::parse( + "[year]-[month]-[day]-[hour][offset_hour sign:mandatory][offset_minute]", + ), + Rotation::DAILY => format_description::parse( + "[year]-[month]-[day][offset_hour sign:mandatory][offset_minute]", + ), + Rotation::WEEKLY => format_description::parse( + "[year]-[month]-[day][offset_hour sign:mandatory][offset_minute]", + ), + Rotation::NEVER => format_description::parse("[year]-[month]-[day]"), + } .expect("Unable to create a formatter; this is a bug in tracing-appender") } }