From e683054697c5904c78be50460e21ea3f4d05a61b Mon Sep 17 00:00:00 2001 From: Gabriel Goller Date: Fri, 7 Jun 2024 14:52:06 +0200 Subject: [PATCH] appender: add fallback to file creation date When using the linux-musl target for rust, the file creation time cannot be retrieved, as the current version does not support it yet ( This will be fixed with [0]). In the meantime, we parse the datetime from the filename and use that as a fallback. Fixes: #2999 [0]: https://github.com/rust-lang/rust/pull/125692 --- tracing-appender/src/rolling.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/tracing-appender/src/rolling.rs b/tracing-appender/src/rolling.rs index d82646933a..3d46999e5d 100644 --- a/tracing-appender/src/rolling.rs +++ b/tracing-appender/src/rolling.rs @@ -34,7 +34,7 @@ use std::{ path::{Path, PathBuf}, sync::atomic::{AtomicUsize, Ordering}, }; -use time::{format_description, Date, Duration, OffsetDateTime, Time}; +use time::{format_description, Date, Duration, OffsetDateTime, PrimitiveDateTime, Time}; mod builder; pub use builder::{Builder, InitError}; @@ -602,7 +602,24 @@ impl Inner { return None; } - let created = metadata.created().ok()?; + let created = metadata.created().ok().or_else(|| { + let datetime = if self.log_filename_prefix.is_none() { + PrimitiveDateTime::parse( + filename.split('.').collect::>()[0], + &self.date_format, + ) + .ok()? + .assume_utc() + } else { + PrimitiveDateTime::parse( + filename.split('.').collect::>()[1], + &self.date_format, + ) + .ok()? + .assume_utc() + }; + Some(datetime.into()) + })?; Some((entry, created)) }) .collect::>()