Link to home
Start Free TrialLog in
Avatar of j-a-c-k
j-a-c-kFlag for United States of America

asked on

This solution is good until the file gets too big. How would you recommend creating a wrap around file . That would store only the last 1000 entries?

Keeping information in a log file with PHP seems easy until one realizes that the log will continue to grow.
There seems to be no easy way to simply move the pointer back to the top of the file without truncating the file.
All examples that I can find revolve around one of two methods:
The first method is to start by writing the new log record to a new file, then read in the records one at a time from the old file writing them back to a new file until you have written as many as you want to maximize the log.
The second method is to suck the entire file into memory, plunk the new record in front of it, wack off the excess and write it back to disk.

Method one uses two files and can use lots of time.
Method two uses lots of memory

Is there a best practice here??
ASKER CERTIFIED SOLUTION
Avatar of Hugh Fraser
Hugh Fraser
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cnjuguna
cnjuguna

is your program writing directly to the file? maybe you could include a check before each write that gets file info e.g. filesize and when it approaches a certain limit you can simply rename the file and create a new file.

another alternative may be logrotate (linux).