reading a log file one line at a time depending on timestamp

This is a discussion on "reading a log file one line at a time depending on timestamp" within the PHP Forum section. This forum, and the thread "reading a log file one line at a time depending on timestamp are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Nov 26th, 2007, 14:30
Up'n'Coming Member
Join Date: Oct 2007
Location: london
Age: 25
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
reading a log file one line at a time depending on timestamp

Hi,

Currently I am attepting to create a stats package for my companies website (yes my life would be 100% stress free if I was allowed to use a standard/commercial stats package ).

So far things are all good, and ive progressed quite far. But ive hit a snag.

At the moment im reading in the WHOLE log file and sorting it into an array and passing the neccesary data to mysql. Easy peasy.

But my boss has pointed out to me that as the site gets more and more traffic the log gets bigger, and if it gets to big the servers memory will overload, and we all know what happens then!

So what I need to do is read in the file one line at a time. and compare the timestamp to the last time the script was run. If the timestamp in the log is less than the last timestamp for when the file was run dont read it. If it is more than defiently read it.

so far my code looks like so...

PHP: Select all

$filename '../../logs\W3SVC10019''/ex'date('ymd'). '.log'// Filename Checker
            
    
if (file_exists($filename)) // Does the corresponding filename exist - if yes then
    
{
        
$fp fopen($filename"r"); //Open the server log
        
$content fread($fpfilesize($filename));     // Read the server log    
        
$content explode("\n"$content); // explode into array    
        
$content  array_reverse($content ); // reverse the array
        
foreach($content as $key=>$value)    {

//This is where im sorting all the array data

So what needs to happen is that the script reads each line which look like...

2007-11-22 23:59:59 W3SVC10019 **.***.***.*** GET /images/e-br...

then grab '2007-11-22 23:59:59' and convert to timestamp ( strtotime )
then compare it to the mysql stored last run time of the script.

If the timestamp is less than the mysql version. Ignore
If it is more read it in.

How do I read one line at a time??
But only the first date/time part, and then figure out true/false??


I hope this all makes sense. Ive tried to explain this fully. If it all still does not make sense, please ask for more info!

Ok thanks in advance! Eon201
Reply With Quote

  #2 (permalink)  
Old Dec 4th, 2007, 05:54
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: reading a log file one line at a time depending on timestamp

To read one file at a time use

fgets(); and check if you have reached the end of the file using feof

PHP: Select all



while (!feof($file)) {
        
$line fgets($file4096);
        
// do your date and time checking here. If you want to stop the running through of the file
// just use break;
    

Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
images loading time changes depending on browser thosecars82 Web Page Design 5 May 19th, 2008 14:57
Edit a specific line of a file??? WebNinja PHP Forum 5 Feb 10th, 2008 11:50
[SOLVED] Reading as text file backwards eon201 PHP Forum 4 Nov 8th, 2007 10:53
Syntax error with Ajax file reading Paramiliar JavaScript Forum 0 Aug 12th, 2007 16:48
Time line question autumn_whispers2me Flash & Multimedia Forum 9 May 26th, 2005 00:43


All times are GMT. The time now is 11:40.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43