PHPでApacheのエラーログを解析するにはどうすればよいですか?
質問
Apacheのエラーログを解析または意味のあるスクリプトを作成して、最新のエラーが何であるかを確認したい。私はそこに誰かがこれを行う何かを持っているのか、どこから始めるのかアイデアを持っているのだろうかと思っていましたか?
解決
最初に考慮すべきことがいくつかあります:
- まず、PHPユーザーがApacheのログファイルにアクセスできない場合があります。
- 第二に、PHPとApacheは、ログファイルがどこにあるかを教えてくれません
- 最後に、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
他のヒント
サンプルスクリプトを探している他の人のために、私は何かを一緒に投げました、それは基本を持っています:
<?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>
これを行うphpスクリプトの山があります。例については、Google検索を実行してください。自分でロールバックしたい場合は、他のファイルを読み取ることほど複雑ではありません。ログファイルの場所(httpd.confファイルで定義)とログファイルのフォーマットがあります。フォーマットはhttpd.confにも定義されています
これは、メモリをオーバーロードせずに大きなファイルの後ろから多くの文字を読みやすくする小さなクラスです。テスト設定により、実際にそれを共食いしている動作を確認できます。
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>";
}
?>
admグループはLinuxシステムでログを表示するためのアクセス権を持ち、Apacheからログにアクセスするには、www-dataユーザーを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>
<?
}
?>
biterScriptingを試しましたか?私はシステム管理者であり、ログの解析に使用しています。 univxスタイルのスクリプトです。 biterScripting.com-&gt;無料ダウンロード。