我是php的新手。我试图解析日志文件并期待数组输出。但不幸的是输出是空的,没有错误/警告。
我试着按照这个例子,在这里:http://www.phpclasses.org/package/2596-PHP-Parse-Apache-log-files-in-the-common-log-format.html
以下是代码:
Apache的日志parser.php
<?php
class apache_log_parser
{
var $bad_rows; // Number of bad rows
var $fp; // File pointer
function format_log_line($line)
{
preg_match("/^(\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+) ([^\]]+)\] \"(\S+) (.*?) (\S+)\" (\S+) (\S+) (\".*?\") (\".*?\")$/", $line, $matches); // pattern to format the line
return $matches;
}
function format_line($line)
{
$logs = $this->format_log_line($line); // format the line
if (isset($logs[0])) // check that it formated OK
{
$formated_log = array(); // make an array to store the lin info in
$formated_log['ip'] = $logs[1];
$formated_log['identity'] = $logs[2];
$formated_log['user'] = $logs[2];
$formated_log['date'] = $logs[4];
$formated_log['time'] = $logs[5];
$formated_log['timezone'] = $logs[6];
$formated_log['method'] = $logs[7];
$formated_log['path'] = $logs[8];
$formated_log['protocal'] = $logs[9];
$formated_log['status'] = $logs[10];
$formated_log['bytes'] = $logs[11];
$formated_log['referer'] = $logs[12];
$formated_log['agent'] = $logs[13];
return $formated_log; // return the array of info
}
else
{
$this->bad_rows++; // if the row is not in the right format add it to the bad rows
return false;
}
}
function open_log_file($file_name)
{
$this->fp = fopen($file_name, 'r'); // open the file
if (!$this->fp)
{
return false; // return false on fail
}
return true; // return true on sucsess
}
function close_log_file()
{
return fclose($this->fp); // close the file
}
// gets a line from the log file
function get_line()
{
if (feof($this->fp))
{
return false;
}
$bits='';
// I find for loops much much faster
for (;!feof($this->fp) && $bits != "\n";)
{
$bits .= fread($this->fp, 1);
}
return rtrim($bits, "\n");
}
}
?>
使用example.php
<?php
include 'apache-log-parser.php';
$apache_log_parser = new apache_log_parser(); // Create an apache log parser
if ($apache_log_parser->open_log_file('example.log')) // Make sure it opens the log file
{
while ($line = $apache_log_parser->get_line()) { // while it can get a line
//print_r($line);
$parsed_line = $apache_log_parser->format_line($line); // format the line
//var_export($parsed_line);
// print_r($parsed_line); // print out the array
}
$apache_log_parser->close_log_file(); // close the log file
}
else
{
echo 'Sorry cannot open log file.';
}
?>
example.log
172.0.0.1 - - [21/Sep/2005:23:06:37 +0100] "GET / HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b3) Gecko/20050712 Firefox/1.0+"
172.0.0.1 - - [21/Sep/2005:23:06:38 +0100] "GET / HTTP/1.1" 200 1647 "http://www.example.com/" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b3) Gecko/20050712 Firefox/1.0+"
172.0.0.1 - - [21/Sep/2005:23:06:38 +0100] "GET / HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b3) Gecko/20050712 Firefox/1.0+"
172.0.0.1 - - [21/Sep/2005:23:06:38 +0100] "GET / HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b3) Gecko/20050712 Firefox/1.0+"
172.0.0.1 - - [21/Sep/2005:23:06:39 +0100] "GET / HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b3) Gecko/20050712 Firefox/1.0+"
172.0.0.1 - - [21/Sep/2005:23:06:40 +0100] "GET / HTTP/1.1" 200 12372 "http://www.example.com/" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b3) Gecko/20050712 Firefox/1.0+"
172.0.0.1 - - [21/Sep/2005:23:06:40 +0100] "GET / HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b3) Gecko/20050712 Firefox/1.0+"
172.0.0.1 - - [21/Sep/2005:23:06:41 +0100] "GET / HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b3) Gecko/20050712 Firefox/1.0+"
172.0.0.1 - - [21/Sep/2005:23:06:41 +0100] "GET / HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b3) Gecko/20050712 Firefox/1.0+"
如果有人对此进行调查并建议我做出哪些改变,我将很高兴。