18

apache のエラー ログを解析または解釈して、最新のエラーを確認するスクリプトを作成したいと考えています。誰かがこれを行う何かを持っているか、どこから始めるべきかアイデアを持っているかどうか疑問に思っていましたか?

4

5 に答える 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 には、PHP エラーをログ ファイルにリダイレクトするための ini 設定もあります。error_log = /path/to/error.log

php_flag 表記を使用して、httpd.conf または .htaccess ファイル (アクセスできる場合) でこれを設定できます。

php_flag error_log /web/mysite/logs/error.log
于 2008-10-01T20:03:01.433 に答える
11

サンプルスクリプトを探している他の人のために、私は何かを一緒に投げました、それは基本を持っています:

<?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-02T18:32:17.993 に答える
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>";
}

?> 
于 2010-12-21T03:43:50.350 に答える
3

これを行う php スクリプトが山ほどあります。Google で例を検索してみてください。自分で作成したい場合は、他のファイルを読み取るのと同じくらい複雑です。ログファイルの場所 (httpd.conf ファイルで定義) とログファイルの形式を知っていることを確認してください。形式は httpd.conf でも定義されています。

于 2008-10-01T20:03:58.127 に答える
-2

biterScripting を試しましたか? 私はシステム管理者で、ログの解析に使用しています。univx スタイルのスクリプトです。biterScripting.com -> 無料ダウンロード。

于 2008-12-09T20:37:56.053 に答える