Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/worker/src/Monolog
File: ErrorHandler.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/*
[2] Fix | Delete
* This file is part of the Monolog package.
[3] Fix | Delete
*
[4] Fix | Delete
* (c) Jordi Boggiano <j.boggiano@seld.be>
[5] Fix | Delete
*
[6] Fix | Delete
* For the full copyright and license information, please view the LICENSE
[7] Fix | Delete
* file that was distributed with this source code.
[8] Fix | Delete
*/
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* Monolog error handler
[12] Fix | Delete
*
[13] Fix | Delete
* A facility to enable logging of runtime errors, exceptions and fatal errors.
[14] Fix | Delete
*
[15] Fix | Delete
* Quick setup: <code>ErrorHandler::register($logger);</code>
[16] Fix | Delete
*
[17] Fix | Delete
* @author Jordi Boggiano <j.boggiano@seld.be>
[18] Fix | Delete
*/
[19] Fix | Delete
class Monolog_ErrorHandler
[20] Fix | Delete
{
[21] Fix | Delete
private $logger;
[22] Fix | Delete
[23] Fix | Delete
private $previousExceptionHandler;
[24] Fix | Delete
private $uncaughtExceptionLevel;
[25] Fix | Delete
[26] Fix | Delete
private $previousErrorHandler;
[27] Fix | Delete
private $errorLevelMap;
[28] Fix | Delete
[29] Fix | Delete
private $fatalLevel;
[30] Fix | Delete
private $reservedMemory;
[31] Fix | Delete
private static $fatalErrors = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR);
[32] Fix | Delete
[33] Fix | Delete
public function __construct(Monolog_Psr_LoggerInterface $logger)
[34] Fix | Delete
{
[35] Fix | Delete
$this->logger = $logger;
[36] Fix | Delete
}
[37] Fix | Delete
[38] Fix | Delete
/**
[39] Fix | Delete
* Registers a new ErrorHandler for a given Logger
[40] Fix | Delete
*
[41] Fix | Delete
* By default it will handle errors, exceptions and fatal errors
[42] Fix | Delete
*
[43] Fix | Delete
* @param Monolog_Psr_LoggerInterface $logger
[44] Fix | Delete
* @param array|false $errorLevelMap an array of E_* constant to LogLevel::* constant mapping, or false to disable error handling
[45] Fix | Delete
* @param int|false $exceptionLevel a LogLevel::* constant, or false to disable exception handling
[46] Fix | Delete
* @param int|false $fatalLevel a LogLevel::* constant, or false to disable fatal error handling
[47] Fix | Delete
*
[48] Fix | Delete
* @return Monolog_ErrorHandler
[49] Fix | Delete
*/
[50] Fix | Delete
public static function register(Monolog_Psr_LoggerInterface $logger, $errorLevelMap = array(), $exceptionLevel = null, $fatalLevel = null)
[51] Fix | Delete
{
[52] Fix | Delete
$handler = new self($logger);
[53] Fix | Delete
if ($errorLevelMap !== false) {
[54] Fix | Delete
$handler->registerErrorHandler($errorLevelMap);
[55] Fix | Delete
}
[56] Fix | Delete
if ($exceptionLevel !== false) {
[57] Fix | Delete
$handler->registerExceptionHandler($exceptionLevel);
[58] Fix | Delete
}
[59] Fix | Delete
if ($fatalLevel !== false) {
[60] Fix | Delete
$handler->registerFatalHandler($fatalLevel);
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
return $handler;
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
public function registerExceptionHandler($level = null, $callPrevious = true)
[67] Fix | Delete
{
[68] Fix | Delete
$prev = set_exception_handler(array($this, 'handleException'));
[69] Fix | Delete
$this->uncaughtExceptionLevel = $level;
[70] Fix | Delete
if ($callPrevious && $prev) {
[71] Fix | Delete
$this->previousExceptionHandler = $prev;
[72] Fix | Delete
}
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
public function registerErrorHandler(array $levelMap = array(), $callPrevious = true, $errorTypes = -1)
[76] Fix | Delete
{
[77] Fix | Delete
$prev = set_error_handler(array($this, 'handleError'), $errorTypes);
[78] Fix | Delete
$this->errorLevelMap = $this->arrayReplace($this->defaultErrorLevelMap(), $levelMap);
[79] Fix | Delete
if ($callPrevious) {
[80] Fix | Delete
$this->previousErrorHandler = $prev ? $prev : true;
[81] Fix | Delete
}
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
/**
[85] Fix | Delete
* Backport of PHP >=5.3 function array_replace
[86] Fix | Delete
*
[87] Fix | Delete
* @link http://www.php.net/manual/en/function.array-replace.php#94458
[88] Fix | Delete
*/
[89] Fix | Delete
private function arrayReplace(array $array, array $array1)
[90] Fix | Delete
{
[91] Fix | Delete
$args = func_get_args();
[92] Fix | Delete
$count = func_num_args();
[93] Fix | Delete
[94] Fix | Delete
for ($i = 0; $i < $count; ++$i) {
[95] Fix | Delete
if (is_array($args[$i])) {
[96] Fix | Delete
foreach ($args[$i] as $key => $val) {
[97] Fix | Delete
$array[$key] = $val;
[98] Fix | Delete
}
[99] Fix | Delete
} else {
[100] Fix | Delete
trigger_error(
[101] Fix | Delete
__FUNCTION__.'(): Argument #'.($i + 1).' is not an array',
[102] Fix | Delete
E_USER_WARNING
[103] Fix | Delete
);
[104] Fix | Delete
[105] Fix | Delete
return null;
[106] Fix | Delete
}
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
return $array;
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
public function registerFatalHandler($level = null, $reservedMemorySize = 20)
[113] Fix | Delete
{
[114] Fix | Delete
register_shutdown_function(array($this, 'handleFatalError'));
[115] Fix | Delete
[116] Fix | Delete
$this->reservedMemory = str_repeat(' ', 1024 * $reservedMemorySize);
[117] Fix | Delete
$this->fatalLevel = $level;
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
protected function defaultErrorLevelMap()
[121] Fix | Delete
{
[122] Fix | Delete
$map = array(
[123] Fix | Delete
E_ERROR => Monolog_Psr_LogLevel::CRITICAL,
[124] Fix | Delete
E_WARNING => Monolog_Psr_LogLevel::WARNING,
[125] Fix | Delete
E_PARSE => Monolog_Psr_LogLevel::ALERT,
[126] Fix | Delete
E_NOTICE => Monolog_Psr_LogLevel::NOTICE,
[127] Fix | Delete
E_CORE_ERROR => Monolog_Psr_LogLevel::CRITICAL,
[128] Fix | Delete
E_CORE_WARNING => Monolog_Psr_LogLevel::WARNING,
[129] Fix | Delete
E_COMPILE_ERROR => Monolog_Psr_LogLevel::ALERT,
[130] Fix | Delete
E_COMPILE_WARNING => Monolog_Psr_LogLevel::WARNING,
[131] Fix | Delete
E_USER_ERROR => Monolog_Psr_LogLevel::ERROR,
[132] Fix | Delete
E_USER_WARNING => Monolog_Psr_LogLevel::WARNING,
[133] Fix | Delete
E_USER_NOTICE => Monolog_Psr_LogLevel::NOTICE,
[134] Fix | Delete
E_STRICT => Monolog_Psr_LogLevel::NOTICE,
[135] Fix | Delete
E_RECOVERABLE_ERROR => Monolog_Psr_LogLevel::ERROR,
[136] Fix | Delete
);
[137] Fix | Delete
[138] Fix | Delete
if (version_compare(PHP_VERSION, '5.3', '>=')) {
[139] Fix | Delete
$map[E_USER_DEPRECATED] = Monolog_Psr_LogLevel::NOTICE;
[140] Fix | Delete
$map[E_DEPRECATED] = Monolog_Psr_LogLevel::NOTICE;
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
return $map;
[144] Fix | Delete
}
[145] Fix | Delete
[146] Fix | Delete
/**
[147] Fix | Delete
* @private
[148] Fix | Delete
*/
[149] Fix | Delete
public function handleException($e)
[150] Fix | Delete
{
[151] Fix | Delete
$this->logger->log(
[152] Fix | Delete
$this->uncaughtExceptionLevel === null ? Monolog_Psr_LogLevel::ERROR : $this->uncaughtExceptionLevel,
[153] Fix | Delete
'Uncaught exception',
[154] Fix | Delete
array('exception' => $e)
[155] Fix | Delete
);
[156] Fix | Delete
[157] Fix | Delete
if ($this->previousExceptionHandler) {
[158] Fix | Delete
call_user_func($this->previousExceptionHandler, $e);
[159] Fix | Delete
}
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
/**
[163] Fix | Delete
* @private
[164] Fix | Delete
*/
[165] Fix | Delete
public function handleError($code, $message, $file = '', $line = 0, $context = array())
[166] Fix | Delete
{
[167] Fix | Delete
if (!(error_reporting() & $code)) {
[168] Fix | Delete
return;
[169] Fix | Delete
}
[170] Fix | Delete
$level = isset($this->errorLevelMap[$code]) ? $this->errorLevelMap[$code] : Monolog_Psr_LogLevel::CRITICAL;
[171] Fix | Delete
$this->logger->log($level, self::codeToString($code).': '.$message, array('code' => $code, 'message' => $message, 'file' => $file, 'line' => $line));
[172] Fix | Delete
if ($this->previousErrorHandler === true) {
[173] Fix | Delete
return false;
[174] Fix | Delete
} elseif ($this->previousErrorHandler) {
[175] Fix | Delete
return call_user_func($this->previousErrorHandler, $code, $message, $file, $line, $context);
[176] Fix | Delete
}
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
/**
[180] Fix | Delete
* @private
[181] Fix | Delete
*/
[182] Fix | Delete
public function handleFatalError()
[183] Fix | Delete
{
[184] Fix | Delete
$this->reservedMemory = null;
[185] Fix | Delete
[186] Fix | Delete
$lastError = error_get_last();
[187] Fix | Delete
if ($lastError && in_array($lastError['type'], self::$fatalErrors)) {
[188] Fix | Delete
$this->logger->log(
[189] Fix | Delete
$this->fatalLevel === null ? Monolog_Psr_LogLevel::ALERT : $this->fatalLevel,
[190] Fix | Delete
'Fatal Error ('.self::codeToString($lastError['type']).'): '.$lastError['message'],
[191] Fix | Delete
array('code' => $lastError['type'], 'message' => $lastError['message'], 'file' => $lastError['file'], 'line' => $lastError['line'])
[192] Fix | Delete
);
[193] Fix | Delete
}
[194] Fix | Delete
}
[195] Fix | Delete
[196] Fix | Delete
private static function codeToString($code)
[197] Fix | Delete
{
[198] Fix | Delete
switch ($code) {
[199] Fix | Delete
case E_ERROR:
[200] Fix | Delete
return 'E_ERROR';
[201] Fix | Delete
case E_WARNING:
[202] Fix | Delete
return 'E_WARNING';
[203] Fix | Delete
case E_PARSE:
[204] Fix | Delete
return 'E_PARSE';
[205] Fix | Delete
case E_NOTICE:
[206] Fix | Delete
return 'E_NOTICE';
[207] Fix | Delete
case E_CORE_ERROR:
[208] Fix | Delete
return 'E_CORE_ERROR';
[209] Fix | Delete
case E_CORE_WARNING:
[210] Fix | Delete
return 'E_CORE_WARNING';
[211] Fix | Delete
case E_COMPILE_ERROR:
[212] Fix | Delete
return 'E_COMPILE_ERROR';
[213] Fix | Delete
case E_COMPILE_WARNING:
[214] Fix | Delete
return 'E_COMPILE_WARNING';
[215] Fix | Delete
case E_USER_ERROR:
[216] Fix | Delete
return 'E_USER_ERROR';
[217] Fix | Delete
case E_USER_WARNING:
[218] Fix | Delete
return 'E_USER_WARNING';
[219] Fix | Delete
case E_USER_NOTICE:
[220] Fix | Delete
return 'E_USER_NOTICE';
[221] Fix | Delete
case E_STRICT:
[222] Fix | Delete
return 'E_STRICT';
[223] Fix | Delete
case E_RECOVERABLE_ERROR:
[224] Fix | Delete
return 'E_RECOVERABLE_ERROR';
[225] Fix | Delete
case E_DEPRECATED:
[226] Fix | Delete
return 'E_DEPRECATED';
[227] Fix | Delete
case E_USER_DEPRECATED:
[228] Fix | Delete
return 'E_USER_DEPRECATED';
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
if (version_compare(PHP_VERSION, '5.3', '>=')) {
[232] Fix | Delete
switch ($code) {
[233] Fix | Delete
case E_DEPRECATED:
[234] Fix | Delete
return 'E_DEPRECATED';
[235] Fix | Delete
case E_USER_DEPRECATED:
[236] Fix | Delete
return 'E_USER_DEPRECATED';
[237] Fix | Delete
}
[238] Fix | Delete
}
[239] Fix | Delete
[240] Fix | Delete
return 'Unknown PHP error';
[241] Fix | Delete
}
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function