سؤال

أرغب في إنشاء برنامج نصي يقوم بتوزيع سجل أخطاء 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 أيضًا على إعداد ini لإعادة توجيه أخطاء PHP إلى ملف سجل: error_log = /path/to/error.log

يمكنك ضبط هذا في httpd.conf أو في ملف htaccess (إذا كان لديك حق الوصول إلى واحد) باستخدام تدوين php_flag:

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 التي تفعل ذلك، لا مجرد البحث جوجل للحصول على أمثلة. إذا كنت ترغب في القوائم الخاصة بك، فإنه شيء أكثر تعقيدا من قراءة أي ملف آخر. فقط تأكد من معرفة موقع ملفات السجلات الخاصة بك (المحددة في ملف 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 لديه حق الوصول لللعرض السجلات في نظام لينكس للوصول إلى سجل من اباتشي لدينا لإضافة مستخدم على شبكة الاتصالات العالمية البيانات إلى مجموعة ADM وإعادة تشغيل اباتشي لذلك كل التغييرات سيتم تحديث

$ 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