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
You may need to handle the prefix and suffix before parsing the date (a reverse function of join_date).
https://github.com/tokio-rs/tracing/blob/c6bedbe2725830c1e78cbcdb9168de69c98e42fc/tracing-appender/src/rolling.rs#L551-L569
Oh, yeah, you're right. Thanks for the heads up, updated it now. Extracted the the datetime from the prefix/suffix, then converted to PrimitiveDateTime (because OffsetDateTime also takes a offset), then just assume it's utc (this is fine, because we also do this when writing AFAIK).
The prefix or suffix can also contain a dot.
Damn, forgot about that. Should work now!
Third time's the charm :) LMK what you think now!
replacen can occur within a string. We must ensure they are strictly prefixes or suffixes.
What do you mean exactly, could you paste an example? If the logfile prefix given is not actually the prefix in the filename, we will get a wrong date string and the parsing will fail, but this is intended.
I think you could have suffix 2024 and then files like prefix-2024-01-01-2024 will get turned into -01-01-2024.
Oof, right. I could reverse the suffix and the whole filename string and then search through? Like that I would always get the first substring from behind. AFAIK rust doesn't have a function to make it more convention ...
Wouldn't strip_prefix and strip_suffix work instead of replacen?
Hi! what's the status of this PR? Looks like it addressed all the review comments, is it getting blocked for some reason? We'd love to have this fix to avoid using a fork of tracing that includes it, in a downstream project of ours :pray: Thanks for your work here!
@davidbarsky friendly ping :)
not work when rotation is set to Rotation::DAILY, PrimitiveDateTime::parse(datetime, &self.date_format) return None.
PrimitiveDateTime can not parse a str without time part.
let created:Option<SystemTime> = match self.rotation {
Rotation::DAILY => {
let day =Date::parse(datetime, &self.date_format).ok()?;
Some(PrimitiveDateTime::new(day, Time::MIDNIGHT).assume_utc().into())
},
_ => {
Some(PrimitiveDateTime::parse(datetime, &self.date_format)
.ok()?
.assume_utc().into())
}
};