Jump to content

Parsing .log files


Adsa

Recommended Posts

Hi..

 

I need some help with parsing a file.

I am parsing a log file to a db but the file is being written to all the time as well. I am wondering is there php command i can use so when i open the file i can go to the last parsed line of the .log file? or am i going to have to record the last line and match it the continue from there?

 

Thanks in adavance

Link to comment
https://forums.phpfreaks.com/topic/167554-parsing-log-files/
Share on other sites

If you use the file() function to read the file it will create an array, you could therefore count the elements in the array and start your next read at the line 1 greater than the size of the array itself.

 

I personally would suggest renaming the existing log file when parsing it so that any new data is written to a new file.  Therefore you are not worrying about reading for position over and over again.

Link to comment
https://forums.phpfreaks.com/topic/167554-parsing-log-files/#findComment-883529
Share on other sites

$lines = file('myfile.log');
$lastline = $lines[count($lines)-1];

 

or

$lastline = readlastline('myfile.log');
function readlastline($file)
{
       $fp = @fopen($file, "r");
       $pos = -1;
       $t = " ";
       while ($t != "\n") {
             fseek($fp, $pos, SEEK_END);
             $t = fgetc($fp);
             $pos = $pos - 1;
       }
       $t = fgets($fp);
       fclose($fp);
       return $t;
} 

Link to comment
https://forums.phpfreaks.com/topic/167554-parsing-log-files/#findComment-883530
Share on other sites

<?php

$access_file_path = "file.log";
$last="18456";
$count =0
$handle = fopen("$access_file_path", "r") or die("can't open file");
while (!feof($handle)) {
$userinfo = fscanf($handle, "%s %s %s %s %s %s %s %s %s %s %s %s %s \n");
	if ($userinfo) {
		list ($field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8, $field9, $field10, $field11, $field12, $field13) = $userinfo;
		$output = "$field1 $field2 $field3 $field4 $field5 $field6 $field7 $field8 $field9 $field10 $field11 $field12 $field13";
		$output = rtrim($output);
		$count+=1;
		if ($count == $last) {
			$userinfo = fscanf($handle, "%s %s %s %s %s %s %s %s %s %s %s %s %s \n");
			if ($userinfo) {
				list ($field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8, $field9, $field10, $field11, $field12, $field13) = $userinfo;
				$output = "$field1 $field2 $field3 $field4 $field5 $field6 $field7 $field8 $field9 $field10 $field11 $field12 $field13";
				$output = rtrim($output);
				echo "$output"; // do something with output
				}
				$userinfo=NULL;
			}
		}
	$userinfo=NULL;
}
fclose($handle);
?>

Link to comment
https://forums.phpfreaks.com/topic/167554-parsing-log-files/#findComment-884552
Share on other sites

I changed my code slightly but still not able to get it to work.

 

<?php

//get end of file pointer
$fp = fopen("file.log","r");


//set file pointer to end
fseek($fp,0,SEEK_END);

// get pointer
$end_file = ftell($fp); 

// set start of file to scan from
$start_file = 18456;

// set pointer where to start scan
fseek($fp,"$start_file",SEEK_SET);

//display information
while (!feof($fp)) {
$userinfo = fscanf($fp, "%s %s %s %s %s %s %s %s %s %s %s %s %s \n");
	if ($userinfo) {
		list ($field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8, $field9, $field10, $field11, $field12, $field13) = $userinfo;
		$output = "$field1 $field2 $field3 $field4 $field5 $field6 $field7 $field8 $field9 $field10 $field11 $field12 $field13";
		$output = rtrim($output);
		}
	$userinfo=NULL;
}
fclose($fp);
?> 

Link to comment
https://forums.phpfreaks.com/topic/167554-parsing-log-files/#findComment-885642
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.