dongyou9818 2013-05-13 12:12
浏览 58
已采纳

如何在zend框架中记录数据库错误

I am using zend framework 1.12 for my project. I want to catch all types of fatal errors and send them to an email address for quick fix. I have written the below mentioned code in Bootstrap.php file for this purpose.

protected function _initFatalErrorCatcher()
{
    register_shutdown_function(array($this, 'errorlogHandler'));
}

public function errorlogHandler()
{
    $e = error_get_last();

if (!is_null($e)) { //fatal error

    $msg  = 'Fatal error: ' . $e['message'];
    $msg .= ' in' . $e['file'];
    $msg .= ' on line: ' . $e['line'];

    $mail = new Zend_Mail('utf-8');
    $mail->setBodyHtml($msg);
    $mail->setFrom('zzz@z.com');
    $mail->addTo('yyy@y.com');
    $mail->setSubject('check this error');

    $mail->send();
    }
}

Using the above code, i am able to send fatal errors other than database connection related errors and query related errors to email. I followed the instructions from Catch Zend PDO Exception as well, but i believe i am missing something as its not working.

Any help on this will be appreciated.

EDIT:

I am also using Zend_Log to write the error logs in a log-file. But, using this i could not find a way to write the fatal errors. Code for this is given below.

$writer = new Zend_Log_Writer_Stream(APPLICATION_PATH  . "/../data/log-file.log");
$errors = $this->_getParam('error_handler');
$exception = $errors->exception;

$log = new Zend_Log($writer);
$log->debug($exception->getMessage() . "
" . $exception->getTraceAsString());

Scenario for database connection related issue:

If there is any error in host name, database name or in user name, it shows a Fatal error in browser like below. But its not detected by register_shutdown_function() or Zend_Log().

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000] [1044] Access denied for user 'AAAA'@'%' to database 'BBBB'' in /var/www/project_name/library/Zend/Db/Adapter/Pdo/Abstract.php on line 144 PDOException: SQLSTATE[42000] [1044] Access denied for user 'AAAA'@'%' to database 'BBBB' in /var/www/project_name/library/Zend/Db/Adapter/Pdo/Abstract.php on line 129
  • 写回答

3条回答 默认 最新

  • dongxianchu3541 2013-05-14 12:29
    关注

    I have solved it by writing the below mentioned code in Bootstrap.php file.

    protected function _initDbConfig()
    {
      $config = new Zend_Config($this->getOptions());
      $params = $config->database->toArray();
      try {
         $db = Zend_Db::factory('Pdo_Mysql', $params);
         $db->getConnection();
    
      } catch (Zend_Db_Adapter_Exception $e) {
        // perhaps the RDBMS is not running
        // code to send email goes here
      } catch (Zend_Exception $e) {
        // perhaps factory() failed to load the specified Adapter class
        // code to send email goes here         
      }
    }
    

    In application.ini, i have the following code.

    database.host     = "localhost"
    database.username = "AAAA"
    database.password = "*****"
    database.dbname   = "BBBBB"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 镍氢电池充电器设计实物使用原理
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?