Skip to content

Commit 3c23950

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: tokio-rs#2999 [0]: rust-lang/rust#125692
1 parent 382ee01 commit 3c23950

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

tracing-appender/src/rolling.rs

Lines changed: 23 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,28 @@ 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 let (Some(prefix), Some(suffix)) =
607+
(&self.log_filename_prefix, &self.log_filename_suffix)
608+
{
609+
let date = filename
610+
.split(prefix)
611+
.last()?
612+
.split(suffix)
613+
.next()?
614+
.strip_prefix('.')?
615+
.strip_suffix('.')?;
616+
617+
PrimitiveDateTime::parse(date, &self.date_format)
618+
.ok()?
619+
.assume_utc()
620+
} else {
621+
PrimitiveDateTime::parse(filename, &self.date_format)
622+
.ok()?
623+
.assume_utc()
624+
};
625+
Some(datetime.into())
626+
})?;
606627
Some((entry, created))
607628
})
608629
.collect::<Vec<_>>()

0 commit comments

Comments
 (0)