appender: Add fallback to file creation date (#3000)

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
This commit is contained in:
Gabriel Goller 2025-08-30 15:13:06 +02:00 committed by GitHub
parent c036318aa3
commit 20f5b3d8ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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};
@ -676,7 +676,24 @@ impl Inner {
return None;
}
let created = metadata.created().ok()?;
let created = metadata.created().ok().or_else(|| {
let mut datetime = filename;
if let Some(prefix) = &self.log_filename_prefix {
datetime = datetime.strip_prefix(prefix)?;
datetime = datetime.strip_prefix('.')?;
}
if let Some(suffix) = &self.log_filename_suffix {
datetime = datetime.strip_suffix(suffix)?;
datetime = datetime.strip_suffix('.')?;
}
Some(
PrimitiveDateTime::parse(datetime, &self.date_format)
.ok()?
.assume_utc()
.into(),
)
})?;
Some((entry, created))
})
.collect::<Vec<_>>()