通过使用PHP迭代文本文件来提取日志
我有一个脚本,可以使用PHPExcel提取日志并将其打印到Excel文件中,日期的每一天都在每个单独的文件中,例如fo-eg(2013-03-01.txt)。现在,我能够提取日志并打印一些操作和操作计数,例如(搜索、打印、电子邮件等),但操作和计数是整个月的。我想提取日志并获取基于一天的数据,例如每天搜索、打印、电子邮件的操作计数。所以我想做的是从每个文件(2013-01-01.txt,2013-01-02.txt)中提取数据,并打印每天的操作数据。我希望我说得足够清楚,下面是代码中提取和打印整个月数据的部分:通过使用PHP迭代文本文件来提取日志,php,file,loops,logging,phpexcel,Php,File,Loops,Logging,Phpexcel,我有一个脚本,可以使用PHPExcel提取日志并将其打印到Excel文件中,日期的每一天都在每个单独的文件中,例如fo-eg(2013-03-01.txt)。现在,我能够提取日志并打印一些操作和操作计数,例如(搜索、打印、电子邮件等),但操作和计数是整个月的。我想提取日志并获取基于一天的数据,例如每天搜索、打印、电子邮件的操作计数。所以我想做的是从每个文件(2013-01-01.txt,2013-01-02.txt)中提取数据,并打印每天的操作数据。我希望我说得足够清楚,下面是代码中提取和打印整
foreach ($files as $filename)
{
$path = "$root/../request_archive/$filename";
$extracted = "$root/../request_archive/$filename.temp";
$fh = fopen($extracted, "r");
$count = 0;
while (!feof($fh))
{
$line = fgets($fh);
if (preg_match("/^\[id\]/", $line))
{
$count = 0;
$record = $line;
} else {
$count++;
$record .= $line;
if ($count > 1)
{
// echo "count: $count\n";
// echo "\n\n$record\n\n";
}
preg_match("/customer: (.*?)(,|\s\[)/i", $record, $regs);
if (!isset($regs[1]) || $customer_name != $regs[1])
{
preg_match("/client: (.*?),/i", $record, $regs);
if (!isset($regs[1]) || $customer_name != $regs[1])
{
continue;
}
}
preg_match("/\[command\] (.*?) \[/i", $record, $regs);
$log_command = $regs[1];
preg_match("/\[params\] (.*?) \[/i", $record, $regs);
$content = $regs[1];
//preg_match("/\[request_time\] (.*?) \[/i", $record, $regs);
preg_match("/\[request_time\] (.*?) /", $record, $regs);
$request_time = $regs[1];
echo "request_time: $request_time\n";
$request_ts = strtotime($request_time);
if ($start_ts && $start_ts > $request_ts)
continue;
if ($end_ts && $end_ts < $request_ts)
continue;
$data = parse_command($log_command, $content);
$command = $data['command'];
$params = $data['params'];
switch($command)
{
case 'venue search':
$actonCounts['venue search']++;
break;
case 'event search':
$actionCounts['event search']++;
break;
case 'emails sent']:
$actionCounts['emails sent']++;
break;
}
if ($command == 'skip')
continue;
$curr_row++;
if ($save_to_excel)
{
$objPHPExcel->getActiveSheet()->getStyle('A'.$curr_row.':C'.$curr_row)->getFont()->setSize(10);
$objPHPExcel->getActiveSheet()->getStyle('A'.$curr_row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$curr_row, $request_time)
->setCellValue('B'.$curr_row, $command)
->setCellValue('C'.$curr_row, $params)
->setCellValue('D'.$curr_row, $actionCounts['venue search']
->SetCellValue('E'.$curr_row, $actionCounts['event search']
->setCellValue('F'.$curr_row, $actionCounts['emails sent'];
}
if ($save_to_csv)
{
$request = "$request_time\t$command\t$params\n";
fwrite($fd, $request);
}
}
$line = null;
unset($line);
}
fclose($fh);
}
foreach($files作为$filename)
{
$path=“$root/。/request_archive/$filename”;
$extracted=“$root/。/request_archive/$filename.temp”;
$fh=fopen($r);
$count=0;
而(!feof($fh))
{
$line=fgets($fh);
如果(预匹配(“/^\[id\]/”,$line))
{
$count=0;
$record=$line;
}否则{
$count++;
$record.=$line;
如果($count>1)
{
//echo“count:$count\n”;
//echo“\n\n$record\n\n”;
}
预匹配(“/customer:(.*)(,|\s\[)/i”,$record,$regs);
如果(!isset($regs[1])| |$customer\u name!=$regs[1])
{
preg_match(“/client:(.*),/i”,$record,$regs);
如果(!isset($regs[1])| |$customer\u name!=$regs[1])
{
继续;
}
}
preg_match(“/\[command\]”(.*?\[/i“,$record,$regs);
$log_command=$regs[1];
预匹配(“/\[params\](.*?\[/i)”,$record,$regs);
$content=$regs[1];
//preg\u match(“/\[request\u time\]”(.*?\[/i“,$record,$regs);
预匹配(“/\[请求时间\](.*?/”,$record,$regs);
$request_time=$regs[1];
回显“请求时间:$request\u time\n”;
$request\u ts=strotime($request\u time);
如果($start&&$start>$request)
继续;
如果($end_ts&&$end_ts<$request_ts)
继续;
$data=parse_命令($log_命令,$content);
$command=$data['command'];
$params=$data['params'];
开关($命令)
{
“场地搜索”案例:
$actonCounts['VICENUE search']++;
打破
案例“事件搜索”:
$actionCounts['event search']++;
打破
案例“已发送电子邮件”]:
$actionCounts['emails sent']++;
打破
}
如果($command=='skip')
继续;
$curr_row++;
如果($save_to_excel)
{
$objPHPExcel->getActiveSheet()->getStyle('A'.$curr\u行'.:C'.$curr\u行)->getFont()->setSize(10);
$objPHPExcel->getActiveSheet()->getStyle('A'.$curr\u行)->getNumberFormat()->setFormatCode(PHPExcel\u Style\u NumberFormat::FORMAT\u DATE\u XLSX15);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$curr\u行,$request\u时间)
->setCellValue('B'.$curr\u行,$command)
->setCellValue('C'.$curr\u行,$params)
->setCellValue('D'.$curr\u row,$actionCounts['vention search']
->SetCellValue('E'.$curr\u行,$actionCounts['event search']
->setCellValue('F'.$curr_row,$actionCounts['emails sent');
}
如果($save_to_csv)
{
$request=“$request\u time\t$command\t$params\n”;
fwrite($fd,$request);
}
}
$line=null;
未结算($行);
}
fclose($fh);
}
非常感谢您的帮助。您应该将这些记录插入数据库。这将使您的查询更加容易。@Marc,是的,我正在这样做,但这是一个多月的时间,但现在我想根据每天提取这些记录!所以?
where date\u of_record='2013-06-25'
将调出今天的所有记录。和group也可以按年份(记录的日期)、月份(记录的日期)、天(记录的日期)
等进行分类。