我想创建一个脚本来解析或理解apache的错误日志,以查看最近的错误是什么。我想知道有没有人能做到这一点,或者有什么想法从哪里开始?
发布于 2008-10-02 18:32:18
对于其他正在寻找示例脚本的人来说,我把一些东西组合在一起,它有基本的东西:
<?php
exec('tail /usr/local/apache/logs/error_log', $output);
?>
<Table border="1">
<tr>
<th>Date</th>
<th>Type</th>
<th>Client</th>
<th>Message</th>
</tr>
<?
foreach($output as $line) {
// sample line: [Wed Oct 01 15:07:23 2008] [error] [client 76.246.51.127] PHP 99. Debugger->handleError() /home/gsmcms/public_html/central/cake/libs/debugger.php:0
preg_match('~^\[(.*?)\]~', $line, $date);
if(empty($date[1])) {
continue;
}
preg_match('~\] \[([a-z]*?)\] \[~', $line, $type);
preg_match('~\] \[client ([0-9\.]*)\]~', $line, $client);
preg_match('~\] (.*)$~', $line, $message);
?>
<tr>
<td><?=$date[1]?></td>
<td><?=$type[1]?></td>
<td><?=$client[1]?></td>
<td><?=$message[1]?></td>
</tr>
<?
}
?>
</table>
发布于 2008-10-01 20:03:58
有一大堆php脚本可以做到这一点,只要在谷歌上搜索一下就知道了。如果你想使用你自己的文件,这并不比读取任何其他文件更复杂。只需确保您知道日志文件(在httpd.conf文件中定义)和format your log files的位置。该格式也在httpd.conf中定义
发布于 2010-12-21 11:43:50
下面是一个小的类,它使得在没有过载内存的情况下从一个大文件的后面读取大量字符变得很容易。测试设置让您可以看到它正在吞噬自己的行为。
BigFile.php
<?php
$run_test = true;
$test_file = 'BigFile.php';
class BigFile
{
private $file_handle;
/**
*
* Load the file from a filepath
* @param string $path_to_file
* @throws Exception if path cannot be read from
*/
public function __construct( $path_to_log )
{
if( is_readable($path_to_log) )
{
$this->file_handle = fopen( $path_to_log, 'r');
}
else
{
throw new Exception("The file path to the file is not valid");
}
}
/**
*
* 'Finish your breakfast' - Jay Z's homme Strict
*/
public function __destruct()
{
fclose($this->file_handle);
}
/**
*
* Returns a number of characters from the end of a file w/o loading the entire file into memory
* @param integer $number_of_characters_to_get
* @return string $characters
*/
public function getFromEnd( $number_of_characters_to_get )
{
$offset = -1*$number_of_characters_to_get;
$text = "";
fseek( $this->file_handle, $offset , SEEK_END);
while(!feof($this->file_handle))
{
$text .= fgets($this->file_handle);
}
return $text;
}
}
if( $run_test )
{
$number_of_characters_to_get = 100000;
$bf = new BigFile($test_file);
$text = $bf->getFromEnd( $number_of_characters_to_get );
echo "$test_file has the following $number_of_characters_to_get characters at the end:
<br/> <pre>$text</pre>";
}
?>
https://stackoverflow.com/questions/159393
复制相似问题