我该如何在PHP中解析Apache的错误日志?

18

我想创建一个脚本来解析或理解Apache的错误日志以查看最近的错误是什么。我想知道是否有人已经有了这样的脚本,或者有任何开始的想法?

5个回答

15

首先,有几件事情需要考虑:

  1. 首先,您的PHP用户可能无法访问Apache的日志文件。
  2. 其次,PHP和Apache不会告诉您日志文件的位置。
  3. 最后,Apache日志文件可能相当大。

但是,如果这些都不适用于您,您可以使用普通的文件读取命令来完成它。获取最后一个错误的最简单方法是

$contents = @file('/path/to/error.log', FILE_SKIP_EMPTY_LINES);
if (is_array($contents)) {
    echo end($contents);
}
unset($contents);
可能有更好的方法来完成这个任务,不会浪费内存,但我会将其留给读者作为练习。
最后一条评论:PHP也有一个ini设置,用于将PHP错误重定向到日志文件:error_log = /path/to/error.log 您可以在httpd.conf或.htaccess文件中(如果您有访问权限)使用php_flag表示法来设置它。
php_flag error_log /web/mysite/logs/error.log

“读者的练习”,我已经听过多少次了。 ;) - willasaywhat
2
嗯,在这种情况下,意味着我正在工作,没有太多时间编写与工作无关的代码。 :) - Powerlord

12

如果其他人正在寻找示例脚本,我已经整理了一些基础内容:

<?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>

3

有很多PHP脚本可以做到这一点,只需在谷歌上搜索示例即可。如果您想自己编写,请注意了解日志文件的位置(在httpd.conf文件中定义)和日志文件格式。格式也在httpd.conf中定义。


3
这是一个相对小的类,使得从大文件后面读取若干字符而不会过载内存变得容易。测试设置可以让您看到它在自我残杀方面的表现。
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>";
}

?> 

-2
你尝试过 biterScripting 吗?我是一名系统管理员,一直在使用它来解析日志。它是一个类 Unix 风格的脚本语言。biterScripting.com -> 免费下载。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接