Skip to content

Commit

Permalink
tracing-appender: support rolling file using local time
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Oct 15, 2024
1 parent bdbaf80 commit e4dddf1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
5 changes: 5 additions & 0 deletions tracing-appender/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
4 changes: 4 additions & 0 deletions tracing-appender/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions tracing-appender/src/rolling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down

0 comments on commit e4dddf1

Please sign in to comment.