2

通过电子邮件从用户那里获得错误报告令人沮丧。我敢打赌,大多数用户不会向开发人员发送错误报告。因此,我想设置如果用户收到 PHP 错误代码,我想通过电子邮件自动获取错误报告。

有没有办法在 PHP 或 CodeIgniter 中获取用户错误报告?

谢谢你。

4

3 回答 3

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, 'you@youremail.com', $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;  

来源

于 2012-12-30T14:11:23.930 回答
0

PHP 日志通常记录到 apache 错误日志(如果您使用的是 apache),通常位于/var/log/httpd/error_log.

CodeIgniter 的错误日志,如果在 config.php 中启用,默认设置为记录到您的application/log/date.php(其中日期是今天的日期)。

如果您愿意,您可以设置一个 cron,每隔一小时通过电子邮件向您发送一个(或两个)文件中的新更改。或者您可以使用 PHP 中的函数创建自己的 PHP 错误处理程序set_error_handler

编辑:mbinette 粘贴了一个使用 PHPset_error_handler函数的好例子。需要注意的是,这不会让您知道致命的 500 错误,错误日志会。

于 2012-12-30T14:12:18.707 回答
0

您可以查看 codeigniter 关于错误报告的文档 http://ellislab.com/codeigniter/user-guide/general/errors.html

提到了 error_reporting() 函数,也许你可以破解它并处理你的电子邮件。

于 2012-12-30T14:12:40.610 回答