Skip to content

Commit e683054

Browse files
committed
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]: rust-lang/rust#125692
1 parent 382ee01 commit e683054

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

tracing-appender/src/rolling.rs

Lines changed: 19 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};
@@ -602,7 +602,24 @@ impl Inner {
602602
return None;
603603
}
604604

605-
let created = metadata.created().ok()?;
605+
let created = metadata.created().ok().or_else(|| {
606+
let datetime = if self.log_filename_prefix.is_none() {
607+
PrimitiveDateTime::parse(
608+
filename.split('.').collect::<Vec<&str>>()[0],
609+
&self.date_format,
610+
)
611+
.ok()?
612+
.assume_utc()
613+
} else {
614+
PrimitiveDateTime::parse(
615+
filename.split('.').collect::<Vec<&str>>()[1],
616+
&self.date_format,
617+
)
618+
.ok()?
619+
.assume_utc()
620+
};
621+
Some(datetime.into())
622+
})?;
606623
Some((entry, created))
607624
})
608625
.collect::<Vec<_>>()

0 commit comments

Comments
 (0)