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

前端 未结 4 1004
忘掉有多难
忘掉有多难 2021-01-31 21:57

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 tha

4条回答
  •  难免孤独
    2021-01-31 22:19

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

提交回复
热议问题