【问题标题】:How to set custom error handler in Codeigniter to Email errors如何在 Codeigniter 中将自定义错误处理程序设置为电子邮件错误
【发布时间】:2012-10-04 15:18:41
【问题描述】:

我对 PHP 和自定义错误处理程序的设置相当陌生,但我有一些代码在 CI 之外运行良好,但我不知道如何将其集成到 CI 控制器中。我收到一个错误“消息:未定义的属性:错误::$my_error_handler”

我的控制器是:

<?php
class Errors extends CI_Controller {


    public function __construct()
    {
        parent::__construct();

    }

    function my_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, 'name@domain.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.");
        }
    }


    function test()
    {
        // We should use our custom function to handle errors.
        set_error_handler($this->my_error_handler);

        // Trigger an error... (var doesn't exist)
        echo $somevarthatdoesnotexist;

    }

}
?>

如果有更好的方法使用 CI 通过电子邮件发送错误消息,请告诉我。

【问题讨论】:

标签: php codeigniter


【解决方案1】:

我不认为扩展 CI_Controller 是解决这个问题的正确方法。相反,您应该扩展 CI_Logs 以在记录错误时发送电子邮件:

class MY_Log extends CI_Log {

    function MY_Log(){

        parent::__construct();

    }

    function write_log($level = 'error', $msg, $php_error = FALSE){

        $result = parent::write_log($level, $msg, $php_error);

        if ($result == TRUE && strtoupper($level) == 'ERROR') {

            $message = "An error occurred: \n\n";
            $message .= $level.' - '.date($this->_date_fmt). ' --> '.$msg."\n";

            $this->CI =& get_instance();
            $to = $this->CI->config->item('email_admin_address');
            $from_name = $this->CI->config->item('email_general_name');
            $from_address = $this->CI->config->item('email_general_address');

            $subject = 'An error has occured';
            $headers = "From: $from_name <$from_address>" . "\r\n";
            $headers .= 'Content-type: text/plain; charset=utf-8\r\n';

            mail($to, $subject, $message, $headers);

        }

        return $result;

    }

}

将此类保存到 application\libraries\MY_Logs.php 并且不要忘记在 config.php 中启用错误日志记录:$config['log_threshold'] = 1;

WPStorm致敬,他首先提出了这个想法。

【讨论】:

    【解决方案2】:

    我找到了解决办法。

    set_error_handler(array(&$this, 'my_error_handler'));
    

    因为您必须告诉错误处理程序它是您要调用的类方法(set_error_handler(array(&amp;$this, ‘method’)) 的预期含义)而不仅仅是函数(set_error_handler(‘function’) 的预期含义)

    【讨论】:

      【解决方案3】:

      如果您扩展核心异常类,您将能够在其中添加所需的功能。

      Codeigniter forum post on extending that class

      希望对你有帮助

      【讨论】:

      • 嗨,迈克尔。是的,这为我指明了通过电子邮件发送所有错误的正确方向,但是如果控制器的特定方法中发生错误,我还需要能够进行数据库更新。我想我可能不需要调整异常类来做到这一点。你有什么建议?
      猜你喜欢
      • 2011-09-18
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      • 1970-01-01
      • 2015-02-16
      • 2017-12-18
      • 2014-06-07
      相关资源
      最近更新 更多