Php CodeIgniter中的扩展异常
我收到错误致命错误:在第233行的..\core\CodeIgniter.php中找不到类“CI_Controller” 我在application/core/My_Exception.php中创建了一个名为My_Exceptions的类 基本上,当用户从我的站点收到错误时,我希望收到电子邮件Php CodeIgniter中的扩展异常,php,codeigniter,Php,Codeigniter,我收到错误致命错误:在第233行的..\core\CodeIgniter.php中找不到类“CI_Controller” 我在application/core/My_Exception.php中创建了一个名为My_Exceptions的类 基本上,当用户从我的站点收到错误时,我希望收到电子邮件 <?php class My_Exceptions extends CI_Exceptions{ var $CI=""; function __construct(){
<?php
class My_Exceptions extends CI_Exceptions{
var $CI="";
function __construct(){
parent::__construct();
$this->CI =& get_instance();
}
function show_404($page = '', $log_error = TRUE)
{
$heading = "404 Page My Not Found";
$message = "The page you requested was not found.";
// By default we log this, but allow a dev to skip it
if ($log_error)
{
log_message('error', '404 Page Not Found --> '.$page);
}
//Email to Developer
$this->CI->load->library('email');
$uri = $this->CI->uri->uri_string();
$this->CI->email->from('error-donotreply@YOURAPP.com', 'APP Error');
$this->CI->email->to('youremail@example.org');
$this->CI->email->subject('APP Error [severity: '.$severity.']');
$this->CI->email->message("Page not Found. From URL: ".$uri);
$this->CI->email->send();
echo $this->show_error($heading, $message, 'error_404', 404);
exit;
}
}
异常类在主CI_控制器之前加载 这意味着当发生错误时-您不能使用CI发送电子邮件,因为“$This->CI”不存在
你的选择是要么使用原生PHP发送电子邮件,要么我做什么;自动给自己发送前一天的错误日志(使用CRON作业)。这样,您可以每天检查一次所有错误。我知道您已经接受了答案,但另一种方法是扩展CI日志类
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* MY_Log Class
*
* This library extends the native Log library.
* It adds the function to have the log messages being emailed when they have been outputted to the log file.
*
* @package CodeIgniter
* @subpackage Libraries
* @category Logging
* @author Johan Steen
* @link http://wpstorm.net/
*/
class MY_Log extends CI_Log {
private $_reporting_email = 'test@me.com';
private $_subject = 'CI Logger';
/**
* Constructor
*
* @access public
*/
function __construct() {
parent::__construct();
}
/**
* Write Log File
*
* Calls the native write_log() method and then sends an email if a log message was generated.
*
* @access public
* @param string the error level
* @param string the error message
* @param bool whether the error is a native PHP error
* @return bool
*/
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";
$to = $this->_reporting_email;
$subject = $this->_subject;
$headers = 'From: Example Name <no-reply@example.com>' . "\r\n";
$headers .= 'Content-type: text/plain; charset=utf-8\r\n';
mail($to, $subject, $message, $headers);
}
return $result;
}
}
?>
每生成一个404,就收到一封电子邮件,这似乎很疯狂,发电子邮件或手动查看每日日志更有意义(我可以理解不想创建cron作业)。本机mail()
应该很好,当你只是给自己发电子邮件时这没什么大不了的。OP的代码被写入show_404
函数中,但你可能是对的。在这种情况下,我要说的是,为每一个错误发送一封单独的电子邮件更加疯狂,因为会有更多的错误。除非你真的想让你的收件箱像疯了一样爆炸。。。单页加载可能会产生大量404(例如缺少图像),而不仅仅是404错误。我将添加到另一个错误处理程序。404很容易在我的本地主机上测试。谢谢,我要试试PHP本机。这是另一种选择。谢谢