Skip to content

Commit ecb9818

Browse files
kaffarellx3ccd4828
authored andcommitted
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: tokio-rs#2999 [0]: rust-lang/rust#125692
1 parent 0b847a2 commit ecb9818

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

tracing-appender/src/rolling.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use std::{
3434
path::{Path, PathBuf},
3535
sync::atomic::{AtomicUsize, Ordering},
3636
};
37-
use time::{format_description, Date, Duration, OffsetDateTime, Time};
37+
use time::{format_description, Date, Duration, OffsetDateTime, PrimitiveDateTime, Time};
3838

3939
mod builder;
4040
pub use builder::{Builder, InitError};
@@ -657,7 +657,21 @@ impl Inner {
657657
return None;
658658
}
659659

660-
let created = metadata.created().ok()?;
660+
let created = metadata.created().ok().or_else(|| {
661+
let mut datetime = filename;
662+
if let Some(prefix) = &self.log_filename_prefix {
663+
datetime = datetime.strip_prefix(prefix)?;
664+
datetime = datetime.strip_prefix('.')?;
665+
}
666+
if let Some(suffix) = &self.log_filename_suffix {
667+
datetime = datetime.strip_suffix(suffix)?;
668+
datetime = datetime.strip_suffix('.')?;
669+
}
670+
671+
Some(PrimitiveDateTime::parse(datetime, &self.date_format)
672+
.ok()?
673+
.assume_utc().into())
674+
})?;
661675
Some((entry, created))
662676
})
663677
.collect::<Vec<_>>()

0 commit comments

Comments
 (0)