Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/3493' into develop
Browse files Browse the repository at this point in the history
Close #3493
Fixes #3366
  • Loading branch information
weierophinney committed Jan 21, 2013
2 parents 737fe1b + 40ce895 commit 459e0bc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 54 deletions.
110 changes: 58 additions & 52 deletions library/Zend/Log/Logger.php
Expand Up @@ -36,6 +36,40 @@ class Logger implements LoggerInterface
const INFO = 6;
const DEBUG = 7;

/**
* Map native PHP errors to priority
*
* @var array
*/
public static $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,
);

/**
* Registered error handler
*
* @var bool
*/
protected static $registeredErrorHandler = false;

/**
* Registered exception handler
*
* @var bool
*/
protected static $registeredExceptionHandler = false;

/**
* List of priority code => priority (short) name
*
Expand Down Expand Up @@ -80,20 +114,6 @@ class Logger implements LoggerInterface
*/
protected $processorPlugins;

/**
* Registered error handler
*
* @var bool
*/
protected static $registeredErrorHandler = false;

/**
* Registered exception handler
*
* @var bool
*/
protected static $registeredExceptionHandler = false;

/**
* Constructor
*
Expand Down Expand Up @@ -258,7 +278,6 @@ public function setWriters(SplPriorityQueue $writers)
return $this;
}


/**
* Get processor plugin manager
*
Expand Down Expand Up @@ -488,56 +507,43 @@ public function debug($message, $extra = array())
/**
* Register logging system as an error handler to log PHP errors
*
* @link http://www.php.net/manual/en/function.set-error-handler.php
* @link http://www.php.net/manual/function.set-error-handler.php
* @param Logger $logger
* @return bool
* @param bool $continueNativeHandler
* @return mixed Returns result of set_error_handler
* @throws Exception\InvalidArgumentException if logger is null
*/
public static function registerErrorHandler(Logger $logger)
public static function registerErrorHandler(Logger $logger, $continueNativeHandler = false)
{
// Only register once per instance
if (static::$registeredErrorHandler) {
return false;
}

if ($logger === null) {
throw new Exception\InvalidArgumentException('Invalid Logger specified');
}

$errorHandlerMap = 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_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) use ($errorHandlerMap, $logger) {
$errorLevel = error_reporting();
$previous = set_error_handler(
function ($level, $message, $file, $line, $context) use ($logger, $continueNativeHandler) {
$iniLevel = error_reporting();

if ($errorLevel & $errno) {
if (isset($errorHandlerMap[$errno])) {
$priority = $errorHandlerMap[$errno];
} else {
$priority = Logger::INFO;
if ($iniLevel & $level) {
if (isset(Logger::$errorPriorityMap[$level])) {
$priority = Logger::$errorPriorityMap[$level];
} else {
$priority = Logger::INFO;
}
$logger->log($priority, $message, array(
'errno' => $level,
'file' => $file,
'line' => $line,
'context' => $context,
));
}
$logger->log($priority, $errstr, array(
'errno' => $errno,
'file' => $errfile,
'line' => $errline,
'context' => $errcontext
));

return !$continueNativeHandler;
}
});
);

static::$registeredErrorHandler = true;
return true;
return $previous;
}

/**
Expand Down
9 changes: 7 additions & 2 deletions tests/ZendTest/Log/LoggerTest.php
Expand Up @@ -253,11 +253,16 @@ public function testRegisterErrorHandler()
$writer = new MockWriter;
$this->logger->addWriter($writer);

$this->assertTrue(Logger::registerErrorHandler($this->logger));
$previous = Logger::registerErrorHandler($this->logger);
$this->assertNotNull($previous);
$this->assertTrue(false !== $previous);

// check for single error handler instance
$this->assertFalse(Logger::registerErrorHandler($this->logger));

// generate a warning
echo $test;
echo $test; // $test is not defined

Logger::unregisterErrorHandler();
$this->assertEquals($writer->events[0]['message'], 'Undefined variable: test');
}
Expand Down

0 comments on commit 459e0bc

Please sign in to comment.