【问题标题】:Codeigniter catch database errorsCodeigniter 捕获数据库错误
【发布时间】:2013-01-09 13:06:29
【问题描述】:

我正在寻找一种方法来捕获所有数据库错误(如果发生以及何时发生)并向我的电子邮件发送错误报告。对于常规 php 错误,我扩展了 CI_Exceptions 类并添加了我的电子邮件发送代码以及日志记录。但是数据库错误不会通过CI_Exceptions 而是直接从CI_DB_Driver->query() 记录,我不想修改系统文件夹中的任何文件。

此外,我不想围绕应用执行的每个查询编写日志记录代码。

【问题讨论】:

    标签: codeigniter codeigniter-2


    【解决方案1】:

    我很想扩展 CI_Exceptions show_error 方法来捕获通过“error_db”模板传递的任何错误,而不是通过 db 驱动程序进行搜索。

    由于您已经使用电子邮件代码扩展了 CI_Exceptions,因此这似乎是最佳做法。

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

    【讨论】:

      猜你喜欢
      • 2011-12-12
      • 1970-01-01
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 2019-11-12
      • 2016-04-22
      相关资源
      最近更新 更多