tracing icon indicating copy to clipboard operation
tracing copied to clipboard

appender: add fallback to file creation date

Open kaffarell opened this issue 1 year ago • 13 comments

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

kaffarell avatar Jun 07 '24 12:06 kaffarell

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

zerolfx avatar Jun 09 '24 15:06 zerolfx

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).

kaffarell avatar Jun 11 '24 10:06 kaffarell

The prefix or suffix can also contain a dot.

zerolfx avatar Jun 11 '24 14:06 zerolfx

Damn, forgot about that. Should work now!

kaffarell avatar Jun 12 '24 08:06 kaffarell

Third time's the charm :) LMK what you think now!

kaffarell avatar Jun 13 '24 09:06 kaffarell

replacen can occur within a string. We must ensure they are strictly prefixes or suffixes.

zerolfx avatar Jun 13 '24 09:06 zerolfx

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.

kaffarell avatar Jun 13 '24 12:06 kaffarell

I think you could have suffix 2024 and then files like prefix-2024-01-01-2024 will get turned into -01-01-2024.

mladedav avatar Jun 13 '24 13:06 mladedav

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 ...

kaffarell avatar Jun 13 '24 13:06 kaffarell

Wouldn't strip_prefix and strip_suffix work instead of replacen?

mladedav avatar Jun 15 '24 10:06 mladedav

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!

bnjbvr avatar Nov 26 '24 11:11 bnjbvr

@davidbarsky friendly ping :)

kaffarell avatar Dec 06 '24 09:12 kaffarell

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())
                    }
                };

cyt-666 avatar May 19 '25 07:05 cyt-666