From e4dddf1543a9c4a8baaee06d9cd8ae1a4fd78d99 Mon Sep 17 00:00:00 2001 From: messense Date: Tue, 15 Oct 2024 05:01:49 +0000 Subject: [PATCH] tracing-appender: support rolling file using local time --- tracing-appender/Cargo.toml | 5 +++++ tracing-appender/src/lib.rs | 4 ++++ tracing-appender/src/rolling.rs | 13 ++++++++++--- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/tracing-appender/Cargo.toml b/tracing-appender/Cargo.toml index e8f21ff3a..4d9a9b98e 100644 --- a/tracing-appender/Cargo.toml +++ b/tracing-appender/Cargo.toml @@ -20,6 +20,11 @@ keywords = ["logging", "tracing", "file-appender", "non-blocking-writer"] edition = "2018" rust-version = "1.53.0" +[features] + +# Enables support for rolling file using local time +local-time = ["time/local-offset"] + [dependencies] crossbeam-channel = "0.5.5" time = { version = "0.3.2", default-features = false, features = ["formatting", "parsing"] } diff --git a/tracing-appender/src/lib.rs b/tracing-appender/src/lib.rs index 617751d8c..56b3fb97d 100644 --- a/tracing-appender/src/lib.rs +++ b/tracing-appender/src/lib.rs @@ -121,6 +121,10 @@ //! # } //! ``` //! +//! ## Feature Flags +//! +//! - `local-time`: Enables support for rolling file using local time +//! //! ## Supported Rust Versions //! //! `tracing-appender` is built against the latest stable release. The minimum supported diff --git a/tracing-appender/src/rolling.rs b/tracing-appender/src/rolling.rs index 3c9a61a47..50f922fd8 100644 --- a/tracing-appender/src/rolling.rs +++ b/tracing-appender/src/rolling.rs @@ -209,12 +209,19 @@ impl RollingFileAppender { }) } + #[cfg(test)] + #[inline] + fn now(&self) -> OffsetDateTime { + (self.now)() + } + + #[cfg(not(test))] #[inline] fn now(&self) -> OffsetDateTime { - #[cfg(test)] - return (self.now)(); + #[cfg(feature = "local-time")] + return OffsetDateTime::now_local().unwrap_or_else(|_| OffsetDateTime::now_utc()); - #[cfg(not(test))] + #[cfg(not(feature = "local-time"))] OffsetDateTime::now_utc() } }