2014-11-08

Writing errors/exceptions to a log file in zend framework 1

Add to application.ini
resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream = APPLICATION_PATH "/data/logs/application.log"
resources.log.stream.writerParams.mode = "a"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = 5
Add method to application/Bootstrap.php:
/**
* Error handler
*
* @param $errno
* @param $errstr
* @param $errfile
* @param $errline
*
* @throws ErrorException
*/
public function exceptionErrorHandler($errno, $errstr, $errfile, $errline)
{
  throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
and to constructor in the same file:
set_error_handler([$this, 'exceptionErrorHandler']);
Then in application/controllers/ErrorController.php. Find method "errorAction" and replace:
$log->log($this->view->message, $priority, $errors->exception);
$log->log('Request Parameters', $priority, $errors->request->getParams());
with
$logMessage = $errors->exception->getMessage() . PHP_EOL .
    $errors->exception->getTraceAsString() . PHP_EOL .
    'Request Parameters:' . PHP_EOL .
    var_export($errors->request->getParams(), true) . PHP_EOL .
    str_repeat('-', 50) . PHP_EOL;
$log->log($logMessage, $priority, $errors->exception);
And in the same file change $priority = Zend_Log::CRIT; to $priority = Zend_Log::NOTICE;
Don't forget to create application/data/logs/application.log and set writing permissions to it. Exceptions will be written to this file.

No comments:

Post a Comment