----- Original Message ----- From: "Patrick Bierans" > Bob schrieb: >> I used to use the following to log specific errors: >> <?php >> $log = 'rejected'; >> if (filesize($log) > 20000) unlink($log); >> $data = date('d-m-Y H:i:s')." [$fault]\r\n"; >> error_log($data,3,$log); >> ?> >> But, this meant that sometimes there would only be a few errors shown, if >> the file had reached it's 20000 limit and deleted itself. >> >> So I created a rolling road log of a 100. This allows me to view trends >> easier. I'm not sure how efficient this is, or whether it would be suitable >> for high volume? >> <?php >> $log = 'rejected'; >> $data = date('d-m-Y H:i:s')." [$fault]\r\n"; >> if (file_exists($log)) >> { >> $records = file($log); >> array_unshift($records,$data); >> if (count($records) > 100) array_pop($records); >> $fp = fopen($log,'w'); >> foreach ($records as $item) fwrite($fp,$item); >> fclose($fp); >> } >> else >> { >> $fp = fopen($log,'w'); >> fwrite($fp,$data); >> fclose($fp); >> } >> ?> >> >> Has anyone got a better method, or can improve it? >> Regards, Bob E. >> > Hi Bob! > > Of course there are many more ways of doing it. > To tell you the harsh truth: (I hope you can handle it ;) ) > Loading a file into an array and using array shifting and than write > that array into a file and that for every (!) log action - that is > surely one of the worst. ;) > > I have two other ways for you: > > 1) > if filesize of logfile exceeds 20k use fseek() to jump to position > filesize()-10k and write the rest into a new file. > delete old log and rename new file to log file name. This is the fastest. > > I cut down to 10k so you do not have to do it too often. > > If you want it a bit nicer you can start copying after finding a \n for > the first time. > But most times you only look at the tail - so nobody cares. > > Another way is cycling files: > > 2) > If log reaches 10k rename it and start a new file with that name. > "rejected.log" and "rejected.older.log" can be of use. ;) > > THe first is closer to what you tried but the second is far more performant. > > HTH, > Patrick
Hi Patrick, I had a feeling it wouldn't be very efficient, but couldn't think of another way <big grin>. I haven't bothered much with flat files lately. The last time was when you helped me out deleting semaphore files older than x minutes, back in 2005. I still have your emails printed out as reference. Thanks Patrick, I'll try both suggestions. Regards, Bob E. ------------------------------------ Please remember to write your response BELOW the previous text. Community email addresses: Post message: php-list@yahoogroups.com Subscribe: [EMAIL PROTECTED] Unsubscribe: [EMAIL PROTECTED] List owner: [EMAIL PROTECTED] Shortcut URL to this page: http://groups.yahoo.com/group/php-listYahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/php-list/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/php-list/join (Yahoo! ID required) <*> To change settings via email: mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/