Skip to content

Commit

Permalink
appender: add fallback to file creation date
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kaffarell committed Jun 12, 2024
1 parent 382ee01 commit 3c23950
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions tracing-appender/src/rolling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -602,7 +602,28 @@ impl Inner {
return None;
}

let created = metadata.created().ok()?;
let created = metadata.created().ok().or_else(|| {
let datetime = if let (Some(prefix), Some(suffix)) =
(&self.log_filename_prefix, &self.log_filename_suffix)
{
let date = filename
.split(prefix)
.last()?
.split(suffix)
.next()?
.strip_prefix('.')?
.strip_suffix('.')?;

PrimitiveDateTime::parse(date, &self.date_format)
.ok()?
.assume_utc()
} else {
PrimitiveDateTime::parse(filename, &self.date_format)
.ok()?
.assume_utc()
};
Some(datetime.into())
})?;
Some((entry, created))
})
.collect::<Vec<_>>()
Expand Down

0 comments on commit 3c23950

Please sign in to comment.