Truncating log file at certain filesize

Using SD Card and/or SPIFFS is it possible to truncate (for ex.) a log file at say 100K? And have the file truncate the oldest entries when appending newer ones?

I can append easily enough, but it grows and grows and grows. I'm interested in preserving the older entries up to the 100K so wiping the file isn't reasonable. Maybe I need to review how many entries constitute 100K and count lines?

There are a few different log files (NTP activity, resets, pH reading, temp, etc...)


The usual way of doing this is just to write the file to a desired size, close it, rename it to an archive by adding a number (or timestamp) to the end, open a new file for writing and finally delete the oldest archived file if required.

That works. Thanks.