Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tracing-appender: support rolling file using local time #3105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this error handling though, suggestions welcome!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should panic here. Otherwise we would failover "silently" which isn't so nice here.


#[cfg(not(test))]
#[cfg(not(feature = "local-time"))]
OffsetDateTime::now_utc()
}
}
Expand Down
Loading