我有一个脚本,可以提取日志并使用 PHPExcel 将它们打印到 Excel 文件中,日期的每一天都在每个单独的文件中,例如(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);
}
非常感谢任何帮助。