Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm for writing limited number of lines to text file

I have a program where I need to write text lines to a log file very frequently. I would like to limit the number of lines in the log file to 1000. When I write lines to the file, it should append them normally. Once the file reaches 1000 lines, I'd like to get rid of the first line and then append the new one. Does anyone know if there is a way to do this without rewriting the entire file each time?

like image 681
Janz Dott Avatar asked Oct 20 '22 23:10

Janz Dott


1 Answers

Generally it's a little bit better for a case like this to remove more than one line at a time from the beginning.

That is, if your limit is 1000 lines, and you hit 1000 lines, delete the first 300 or so, and then resume writing. That way, you're not performing the delete operation with every single line written thereafter, only every 300 times. If you need to persist 1000 lines, then instead keep up to 1300 and delete 300 when 1300 is reached.

like image 59
Nick Raverty Avatar answered Oct 27 '22 09:10

Nick Raverty