Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

fixed Zend\Log\Logger::registerErrorHandler() doesn't log previous exceptions #3604

Closed
wants to merge 1 commit into from

2 participants

Marc Bennewitz Matthew Weier O'Phinney
Marc Bennewitz

This PR logs each exception as a single message of a list of exceptions structured as previous exceptions.

Additions:

  • Map severity of instances of ErrorException into a priority like the error handler does
  • added the first test for the exceptions handler

Info for merging into 2.1:
The internal array $errorPriorityMap should be replaced with the static Logger::$errorPriorityMap

Matthew Weier O'Phinney weierophinney referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney [#3604] CS and logic cleanup
- Extract common error priority map to static property
- Use descriptive names instead of abbreviations
3b0b5dd
Matthew Weier O'Phinney weierophinney referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/3604' into develop
Forward port #3604

Conflicts:
	library/Zend/Log/Logger.php
	tests/ZendTest/Log/LoggerTest.php
ca1b933
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney [#3604] CS and logic cleanup
- Extract common error priority map to static property
- Use descriptive names instead of abbreviations
429c79e
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/3604'
Close #3604
585a30f
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/3604' into develop
Forward port #3604

Conflicts:
	library/Zend/Log/Logger.php
	tests/ZendTest/Log/LoggerTest.php
065c129
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 29, 2013
  1. Marc Bennewitz

    Zend\Log\Logger::registerErrorHandler: Log previous exceptions and ma…

    marc-mabe authored
    …p severity of ErrorExceptions to priority + tests
This page is out of date. Refresh to see the latest.
Showing with 87 additions and 9 deletions.
  1. +44 −9 library/Zend/Log/Logger.php
  2. +43 −0 tests/ZendTest/Log/LoggerTest.php
53 library/Zend/Log/Logger.php
View
@@ -11,6 +11,7 @@
namespace Zend\Log;
use DateTime;
+use ErrorException;
use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\SplPriorityQueue;
@@ -427,17 +428,51 @@ public static function registerExceptionHandler(Logger $logger)
throw new Exception\InvalidArgumentException('Invalid Logger specified');
}
- set_exception_handler(function ($exception) use ($logger) {
- $extra = array(
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'trace' => $exception->getTrace()
- );
- if (isset($exception->xdebug_message)) {
- $extra['xdebug'] = $exception->xdebug_message;
+ $errorPriorityMap = array(
+ E_NOTICE => self::NOTICE,
+ E_USER_NOTICE => self::NOTICE,
+ E_WARNING => self::WARN,
+ E_CORE_WARNING => self::WARN,
+ E_USER_WARNING => self::WARN,
+ E_ERROR => self::ERR,
+ E_USER_ERROR => self::ERR,
+ E_CORE_ERROR => self::ERR,
+ E_RECOVERABLE_ERROR => self::ERR,
+ E_STRICT => self::DEBUG,
+ E_DEPRECATED => self::DEBUG,
+ E_USER_DEPRECATED => self::DEBUG,
+ );
+ set_exception_handler(function ($exception) use ($logger, $errorPriorityMap) {
+ $logMessages = array();
+
+ do {
+ $prio = Logger::ERR;
+ if ($exception instanceof ErrorException && isset($errorPriorityMap[$exception->getSeverity()])) {
+ $prio = $errorPriorityMap[$exception->getSeverity()];
+ }
+
+ $extra = array(
+ 'file' => $exception->getFile(),
+ 'line' => $exception->getLine(),
+ 'trace' => $exception->getTrace(),
+ );
+ if (isset($exception->xdebug_message)) {
+ $extra['xdebug'] = $exception->xdebug_message;
+ }
+
+ $logMessages[] = array(
+ 'prio' => $prio,
+ 'msg' => $exception->getMessage(),
+ 'extra' => $extra,
+ );
+ $exception = $exception->getPrevious();
+ } while ($exception);
+
+ foreach (array_reverse($logMessages) as $logMessage) {
+ $logger->log($logMessage['prio'], $logMessage['msg'], $logMessage['extra']);
}
- $logger->log(Logger::ERR, $exception->getMessage(), $extra);
});
+
static::$registeredExceptionHandler = true;
return true;
}
43 tests/ZendTest/Log/LoggerTest.php
View
@@ -10,6 +10,8 @@
namespace ZendTest\Log;
+use Exception;
+use ErrorException;
use Zend\Log\Logger;
use Zend\Log\Writer\Mock as MockWriter;
use Zend\Log\Filter\Mock as MockFilter;
@@ -252,11 +254,52 @@ public function testRegisterErrorHandler()
$this->logger->addWriter($writer);
$this->assertTrue(Logger::registerErrorHandler($this->logger));
+
// check for single error handler instance
$this->assertFalse(Logger::registerErrorHandler($this->logger));
+
// generate a warning
echo $test;
+
Logger::unregisterErrorHandler();
+
$this->assertEquals($writer->events[0]['message'], 'Undefined variable: test');
}
+
+ public function testExceptionHandler()
+ {
+ $writer = new MockWriter;
+ $this->logger->addWriter($writer);
+
+ $this->assertTrue(Logger::registerExceptionHandler($this->logger));
+
+ // check for single error handler instance
+ $this->assertFalse(Logger::registerExceptionHandler($this->logger));
+
+ // get the internal exception handler
+ $exceptionHandler = set_exception_handler(function ($e) {});
+ set_exception_handler($exceptionHandler);
+
+ // reset the exception handler
+ Logger::unregisterExceptionHandler();
+
+ // call the exception handler
+ $exceptionHandler(new Exception('error', 200, new Exception('previos', 100)));
+ $exceptionHandler(new ErrorException('user notice', 1000, E_USER_NOTICE, __FILE__, __LINE__));
+
+ // check logged messages
+ $expectedEvents = array(
+ array('priority' => Logger::ERR, 'message' => 'previos', 'file' => __FILE__),
+ array('priority' => Logger::ERR, 'message' => 'error', 'file' => __FILE__),
+ array('priority' => Logger::NOTICE, 'message' => 'user notice', 'file' => __FILE__),
+ );
+ for ($i = 0; $i < count($expectedEvents); $i++) {
+ $expectedEvent = $expectedEvents[$i];
+ $event = $writer->events[$i];
+
+ $this->assertEquals($expectedEvent['priority'], $event['priority'], 'Unexpected priority');
+ $this->assertEquals($expectedEvent['message'], $event['message'], 'Unexpected message');
+ $this->assertEquals($expectedEvent['file'], $event['extra']['file'], 'Unexpected file');
+ }
+ }
}
Something went wrong with that request. Please try again.