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

Commit

Permalink
[#3604] CS and logic cleanup
Browse files Browse the repository at this point in the history
- Extract common error priority map to static property
- Use descriptive names instead of abbreviations
  • Loading branch information
weierophinney committed Jan 29, 2013
1 parent ba23bed commit 429c79e
Showing 1 changed file with 33 additions and 36 deletions.
69 changes: 33 additions & 36 deletions library/Zend/Log/Logger.php
Expand Up @@ -37,6 +37,26 @@ class Logger implements LoggerInterface
const INFO = 6;
const DEBUG = 7;

/**
* Map of PHP error constants to log priorities
*
* @var array
*/
protected 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,
);

/**
* List of priority code => priority (short) name
*
Expand Down Expand Up @@ -363,22 +383,11 @@ public static function registerErrorHandler(Logger $logger)
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) {
$errorHandlerMap = static::$errorPriorityMap;

set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext)
use ($errorHandlerMap, $logger)
{
$errorLevel = error_reporting();

if ($errorLevel & $errno) {
Expand Down Expand Up @@ -428,27 +437,15 @@ public static function registerExceptionHandler(Logger $logger)
throw new Exception\InvalidArgumentException('Invalid Logger specified');
}

$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,
);
$errorPriorityMap = static::$errorPriorityMap;

set_exception_handler(function ($exception) use ($logger, $errorPriorityMap) {
$logMessages = array();

do {
$prio = Logger::ERR;
$priority = Logger::ERR;
if ($exception instanceof ErrorException && isset($errorPriorityMap[$exception->getSeverity()])) {
$prio = $errorPriorityMap[$exception->getSeverity()];
$priority = $errorPriorityMap[$exception->getSeverity()];
}

$extra = array(
Expand All @@ -461,15 +458,15 @@ public static function registerExceptionHandler(Logger $logger)
}

$logMessages[] = array(
'prio' => $prio,
'msg' => $exception->getMessage(),
'extra' => $extra,
'priority' => $priority,
'message' => $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($logMessage['priority'], $logMessage['message'], $logMessage['extra']);
}
});

Expand Down

0 comments on commit 429c79e

Please sign in to comment.