title: How to log database errors in zend framework
body: <p>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.</p>
  
  <pre><code>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-&gt;setBodyHtml($msg);
      $mail-&gt;setFrom('zzz@z.com');
      $mail-&gt;addTo('yyy@y.com');
      $mail-&gt;setSubject('check this error');
  
      $mail-&gt;send();
      }
  }
  </code></pre>
  
  <p>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 <a href="https://stackoverflow.com/questions/4345274/catch-zend-pdo-exception">Catch Zend PDO Exception</a> as well, but i believe i am missing something as its not working.</p>
  
  <p>Any help on this will be appreciated.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>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.</p>
  
  <pre><code>$writer = new Zend_Log_Writer_Stream(APPLICATION_PATH  . "/../data/log-file.log");
  $errors = $this-&gt;_getParam('error_handler');
  $exception = $errors-&gt;exception;
  
  $log = new Zend_Log($writer);
  $log-&gt;debug($exception-&gt;getMessage() . "\n" . $exception-&gt;getTraceAsString());
  </code></pre>
  
  <p><strong>Scenario for database connection related issue:</strong></p>
  
  <p>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().</p>
  
  <pre><code>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
  </code></pre>