-->

如何可以解析Apache的错误日志中的PHP?(How can I parse Apache'

2019-08-21 15:20发布

我想创建一个解析或使Apache的错误日志的感觉,看看有什么最新的错误是一个脚本。 我不知道是否有人在那里有东西做这个或有任何想法从哪里开始?

Answer 1:

有首先要考虑几件事情:

  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

您可以使用的php_flag符号在httpd.conf或.htaccess文件设置这个(如果你有机会到一个):

php_flag error_log /web/mysite/logs/error.log


Answer 2:

为别人寻找一个示例脚本,我扔东西在一起,它有最基础的:

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


Answer 3:

有PHP脚本,这样做,只是做了谷歌搜索的实例,桩。 如果你想推出自己的,这没有什么比阅读任何其他文件更加复杂。 只是确保你知道你的日志文件的位置(在httpd.conf文件中定义)和日志文件格式是该格式在httpd.conf中还定义



Answer 4:

这里有一个小十岁上下的类,它可以很容易地从一个大文件的后面W / O过载存储器中读取字符数。 测试设置可以让你看到它在行动本身的蚕食。

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>";
}

?> 


Answer 5:

ADM集团拥有的在Linux系统上查看日志访问Apache的日志中,我们必须www数据用户添加到ADM组并重新启动Apache,这样所有的更改将更新访问

$ sudo usermod -G adm www-data
$ sudo service apache2 restart

<?php
 exec('tail /var/log/apache2/error_log', $output);
?>
<Table border="1">
<tr>
    <th>Date</th>
    <th>Type</th>
    <th>Client</th>
    <th>Message</th>
</tr>
<?php
foreach($output as $line) {
    // sample line: [Mon Apr 01 07:23:14.217466 2019] [autoindex:error] [pid 19261] [client 114.143.38.172:55801] AH01276: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>
    <?
}
?>



Answer 6:

您是否尝试过biterScripting? 我是一个系统管理员,我一直用它来分析日志。 这是univx风格的脚本。 biterScripting.com - >免费下载。



文章来源: How can I parse Apache's error log in PHP?