2008-10-01 12 views
16

मैं एक ऐसी स्क्रिप्ट बनाना चाहता हूं जो सबसे हालिया त्रुटि क्या है, यह देखने के लिए अपाचे के त्रुटि लॉग को पार्स या समझ में आता है। मैं सोच रहा था कि क्या किसी के पास ऐसा कुछ है जो ऐसा करता है या कहां से शुरू करने के लिए कोई विचार है?मैं PHP में अपाचे के त्रुटि लॉग को कैसे पार्स कर सकता हूं?

उत्तर

13

कुछ चीजें पहले विचार करने के लिए कर रहे हैं:

  1. सबसे पहले, अपने PHP उपयोगकर्ता अपाचे की लॉग फ़ाइल के लिए पहुँच नहीं कर सकते हैं।
  2. दूसरा, PHP और अपाचे आपको यह बताने वाले नहीं हैं कि लॉग फ़ाइल कहां है,
  3. आखिरकार, अपाचे लॉग फ़ाइलें काफी बड़ी हो सकती हैं।

हालांकि, यदि इनमें से कोई भी लागू नहीं होता है, तो आप इसे करने के लिए सामान्य फ़ाइल पढ़ने के आदेशों का उपयोग कर सकते हैं। सबसे आसान तरीका है पिछले त्रुटि प्राप्त करने के लिए

$contents = @file('/path/to/error.log', FILE_SKIP_EMPTY_LINES); 
if (is_array($contents)) { 
    echo end($contents); 
} 
unset($contents); 

शायद यह है कि स्मृति को oink नहीं है ऐसा करने का एक बेहतर तरीका है, लेकिन मैं पाठक के लिए एक व्यायाम के रूप कि छोड़ देंगे।

एक आखिरी टिप्पणी: पीएचपी भी एक लॉग फ़ाइल के लिए PHP पुनर्निर्देशन त्रुटियों के एक आरं सेटिंग है: error_log = /path/to/error.log

आप (यदि आप एक के लिए उपयोग किया है) httpd.conf में या किसी .htaccess फ़ाइल में इस सेट कर सकते हैं php_flag अंकन का उपयोग:

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

"पाठक के लिए एक अभ्यास", ओह मैंने कितनी बार सुना है। ;) – willasaywhat

+2

ठीक है, इस मामले में इसका मतलब है कि मैं काम पर हूं और गैर-कार्य संबंधित कोड लिखने में बहुत समय नहीं है। :) – Powerlord

3

वहाँ php स्क्रिप्ट है कि ऐसा करने के ढेर कर रहे हैं, सिर्फ उदाहरण के लिए एक गूगल खोज करते हैं। यदि आप अपना खुद का रोल करना चाहते हैं, तो यह किसी भी अन्य फाइल को पढ़ने से ज्यादा जटिल नहीं है। बस सुनिश्चित करें कि आप अपने logfiles (httpd.conf फ़ाइल में परिभाषित) के स्थान और format your log files में हैं। प्रारूप को httpd.conf

10

किसी भी नमूना स्क्रिप्ट की तलाश में किसी भी चीज़ के लिए परिभाषित किया गया है, मैंने कुछ फेंक दिया साथ में, इसे मूल बातें मिली हैं:

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

क्या आपने बिटरस्क्रिप्टिंग की कोशिश की है? मैं एक सिस्टम व्यवस्थापक हूं और मैं पार्स लॉग के लिए उपयोग कर रहा हूं। यह univx शैली स्क्रिप्टिंग है। biterScripting.com -> मुफ्त डाउनलोड करें।

3

यहां एक छोटी-आईश कक्षा है जो बड़ी फ़ाइल 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>"; 
} 

?> 
संबंधित मुद्दे