In CodeIgniter, How Can I Have PHP Error Messages Emailed to Me?

时光毁灭记忆、已成空白 提交于 2019-12-03 04:28:35

问题


I'd like to receive error logs via email. For example, if a Warning-level error message should occur, I'd like to get an email about it.

How can I get that working in CodeIgniter?


回答1:


You could extend the Exception core class to do it.

Might have to adjust the reference to CI's email class, not sure if you can instantiate it from a library like this. I don't use CI's email class myself, I've been using the Swift Mailer library. But this should get you on the right path.

Make a file MY_Exceptions.php and place it in /application/libraries/ (Or in /application/core/ for CI 2)

class MY_Exceptions extends CI_Exceptions {

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

    function log_exception($severity, $message, $filepath, $line)

    {   
        if (ENVIRONMENT === 'production') {
            $ci =& get_instance();

            $ci->load->library('email');
            $ci->email->from('your@example.com', 'Your Name');
            $ci->email->to('someone@example.com');
            $ci->email->cc('another@another-example.com');
            $ci->email->bcc('them@their-example.com');
            $ci->email->subject('error');
            $ci->email->message('Severity: '.$severity.'  --> '.$message. ' '.$filepath.' '.$line);
            $ci->email->send();
        }


        parent::log_exception($severity, $message, $filepath, $line);
    }

}



回答2:


One thing that is left out of the solution is that you have to grab CodeIgniters super object to load and use the email library (or any of CodeIgniters other libraries and native functions).

$CI =& get_instance();

After you have done that you use $CI instead of $this to load the email library and set all of the parameters. For more information click here and look under the Utilizing CodeIgniter Resources within Your Library section.




回答3:


I set up an open source github project to implement this solution with configurable email parameters.




回答4:


Oh, another option is to get a logrotation application that supports emailing digests. Not sure what platform you are on, but you could just have something monitor the error_log file and send you updates, might not be as neat and certainly you would be limited to only information in the error_log. (error_log is Apache, CI has a /logs/ folder in system, and IIS has the Windows Events)




回答5:


I'm just about to release an open source project that does this, and more. It collects errors, sends them to an issue tracker, detects duplicates, turns them into issues and emails staff.

Details are at https://sourceforge.net/news/?group_id=317819&id=293422 and the version 0.1.7 it mentions is due out in a couple of days.

The open source tracker is at http://elastik.sourceforge.net/

Any feedback welcome, Thanks



来源:https://stackoverflow.com/questions/260597/in-codeigniter-how-can-i-have-php-error-messages-emailed-to-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!