PHP에서 Apache의 오류 로그를 구문 분석하려면 어떻게해야합니까?

StackOverflow https://stackoverflow.com/questions/159393

  •  03-07-2019
  •  | 
  •  

문제

가장 최근의 오류가 무엇인지 확인하기 위해 Apache의 오류 로그를 구문 분석하거나 이해하는 스크립트를 만들고 싶습니다. 나는 누군가가 이것을하는 일이 있거나 어디서부터 시작 해야하는지 궁금합니다.

도움이 되었습니까?

해결책

먼저 고려해야 할 몇 가지 사항이 있습니다.

  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

httpd.conf 또는 php_flag 표기법을 사용하여 .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 파일에 정의 됨)와 로그 파일을 형식화합니다 in. 형식은 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 Group은 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-> 무료 다운로드.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top