2008-10-01 45 views

回答

13

有幾件事情需要首先考慮:

  1. 首先,你的PHP用戶可能無法訪問到Apache的日誌文件。其次,PHP和Apache不會告訴你所說的日誌文件在哪裏,
  2. 最後,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 
+0

「給讀者一個練習題」,哦,聽過多少次了。 ;) – willasaywhat 2008-10-01 20:26:25

3

有一堆PHP腳本這樣做,只是做一個谷歌搜索的例子。如果你想推出自己的產品,那麼它不會比閱讀其他文件更復雜。只要確保你知道你的日誌文件的位置(在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

你試過biterScripting?我是系統管理員,我一直在使用解析日誌。它是univx風格的腳本。 biterScripting.com - >免費下載。

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>"; 
} 

?>