Skip to content

Commit 724f3f9

Browse files
kaffarelljplatte
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 c036318 commit 724f3f9

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};
@@ -676,7 +676,21 @@ impl Inner {
676676
return None;
677677
}
678678

679-
let created = metadata.created().ok()?;
679+
let created = metadata.created().ok().or_else(|| {
680+
let mut datetime = filename;
681+
if let Some(prefix) = &self.log_filename_prefix {
682+
datetime = datetime.strip_prefix(prefix)?;
683+
datetime = datetime.strip_prefix('.')?;
684+
}
685+
if let Some(suffix) = &self.log_filename_suffix {
686+
datetime = datetime.strip_suffix(suffix)?;
687+
datetime = datetime.strip_suffix('.')?;
688+
}
689+
690+
Some(PrimitiveDateTime::parse(datetime, &self.date_format)
691+
.ok()?
692+
.assume_utc().into())
693+
})?;
680694
Some((entry, created))
681695
})
682696
.collect::<Vec<_>>()

0 commit comments

Comments
 (0)