Codeigniter捕获数据库错误
我正在寻找一种方法来捕获所有的数据库错误(如果和它们发生时),并发送错误报告我的电子邮件。对于正常的PHP错误,我扩展了 CI_Exceptions
类,并添加了我的电子邮件发送代码除了日志。但是数据库错误不会进入 CI_Exceptions
,而是直接从 CI_DB_Driver-> query()
我不想修改系统文件夹中的任何文件。
I'm looking for a way to catch all the database errors (if and when they occur) and sent an error report my email. For regular php error I extended the CI_Exceptions
class and added my email sending code in addition to logging. But the database errors don't go trough CI_Exceptions
but instead are logged directly from CI_DB_Driver->query()
and I don't want to modify any file in the System folder.
此外,我不想在应用程序的每个查询周围写入日志记录代码。
Also I don't want to write logging code around every query the app does.
我试图扩展CI_Exceptions show_error方法来捕获通过'error_db'模板传递的任何错误,而不是通过db驱动程序。
I'd be tempted to extend CI_Exceptions show_error method to catch any errors passed with the 'error_db' template rather than hunting through the db driver.
由于您已使用电子邮件代码扩展CI_Exceptions,这似乎是最佳做法。
As you've already extended CI_Exceptions with email code this would seem like the best practice.
function show_error($heading, $message, $template = 'error_general', $status_code = 500)
{
set_status_header($status_code);
$message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';
if ($template == 'error_db')
{
// do email send here with $message
}
if (ob_get_level() > $this->ob_level + 1)
{
ob_end_flush();
}
ob_start();
include(APPPATH.'errors/'.$template.'.php');
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}