2012-12-30 33 views
2

通過電子郵件獲取用戶的錯誤報告令人沮喪。我敢打賭,大多數用戶不會向開發者發送錯誤報告。因此,我想設置,如果用戶得到PHP錯誤代碼,我想通過電子郵件自動獲取錯誤報告。Codeigniter,有沒有辦法自動獲取用戶錯誤報告?

有沒有辦法在PHP或CodeIgniter中獲取用戶錯誤報告?

謝謝。

+0

我們在這裏談論什麼樣的錯誤? – itachi

回答

1

你可以做這樣的事情:

<?php 
// Our custom error handler 
function email_error_handler($number, $message, $file, $line, $vars) 
{ 
    $email = " 
     <p>An error ($number) occurred on line 
     <strong>$line</strong> and in the <strong>file: $file.</strong> 
     <p> $message </p>"; 
    $email .= "<pre>" . print_r($vars, 1) . "</pre>"; 
    $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
    // Email the error to someone... 
    error_log($email, 1, '[email protected]', $headers); 
    // Make sure that you decide how to respond to errors (on the user's side) 
    // Either echo an error message, or kill the entire project. Up to you... 
    // The code below ensures that we only "die" if the error was more than 
    // just a NOTICE. 
    if (($number !== E_NOTICE) && ($number < 2048)) { 
     die("There was an error. Please try again later."); 
    } 
} 
// We should use our custom function to handle errors. 
set_error_handler('email_error_handler'); 
// Trigger an error... (var doesn't exist) 
echo $somevarthatdoesnotexist; 

Source

0

PHP日誌通常Apache的錯誤日誌(如果你使用Apache),這通常是在/var/log/httpd/error_log.

CodeIgniter的如果在config.php中啓用,錯誤日誌默認設置爲日誌記錄到您的application/log/date.php(其中日期是今天的日期)。

如果您願意,您可以設置一個cron,每隔一小時發送一封電子郵件給您(或兩個)文件中的新更改。或者您可以使用PHP中的set_error_handler函數創建您自己的PHP錯誤處理程序。

編輯:mbinette已經粘貼了一個使用PHP的set_error_handler函數的好例子。一個警告,這不會讓你知道致命的500錯誤,錯誤日誌會。

0

你可以看看CodeIgniter的有關錯誤文檔報告 http://ellislab.com/codeigniter/user-guide/general/errors.html

有一個關於使用error_reporting()函數,也許你可以破解它,做你的電子郵件的東西提。

+1

實際上,'error_reporting'只用於設置錯誤報告級別。要做電子郵件,你必須使用'set_error_handler' – mbinette

相關問題