I’m writing an application which will use a log file. What I want to do is when the application starts it will check to see if the log file is above a certain size and if it is it will delete ‘x’ amounts of lines from the top of the log file to shorten it up. What would be a good way of going about doing this? Would it be easier to write the most recent entries to the top of the file and then delete from the bottom when I do delete?
Share
So you have a log file, let’s call it “
log.log“First, move
log.logtolog.log.bak. Open it for reading. Read line by line until you have read x number of lines. Open a new file,log.log, for writing. Continue reading lines fromlog.log.bakand for each line write it tolog.log. When there are no more lines, closelog.logandlog.log.bakand then delete log.log.bak.Some pseudo code: