tokio-rs / tracing

Application level tracing for Rust.
https://tracing.rs
MIT License
5.14k stars 671 forks source link

appender: add fallback to file creation date #3000

Open kaffarell opened 4 weeks ago

kaffarell commented 4 weeks ago

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

zerolfx commented 3 weeks ago

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

kaffarell commented 3 weeks ago

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

zerolfx commented 3 weeks ago

The prefix or suffix can also contain a dot.

kaffarell commented 3 weeks ago

Damn, forgot about that. Should work now!

kaffarell commented 3 weeks ago

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

zerolfx commented 3 weeks ago

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

kaffarell commented 3 weeks ago

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.

mladedav commented 3 weeks ago

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

kaffarell commented 3 weeks ago

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

mladedav commented 2 weeks ago

Wouldn't strip_prefix and strip_suffix work instead of replacen?