diff options
Diffstat (limited to 'src/Monolog/ErrorHandler.php')
-rw-r--r-- | src/Monolog/ErrorHandler.php | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/src/Monolog/ErrorHandler.php b/src/Monolog/ErrorHandler.php index 4038d82..18c783f 100644 --- a/src/Monolog/ErrorHandler.php +++ b/src/Monolog/ErrorHandler.php @@ -32,6 +32,7 @@ class ErrorHandler private $previousErrorHandler; private $errorLevelMap; + private $handleOnlyReportedErrors; private $hasFatalErrorHandler; private $fatalLevel; @@ -54,7 +55,7 @@ class ErrorHandler * @param int|false $fatalLevel a LogLevel::* constant, or false to disable fatal error handling * @return ErrorHandler */ - public static function register(LoggerInterface $logger, $errorLevelMap = [], $exceptionLevelMap = [], $fatalLevel = null) + public static function register(LoggerInterface $logger, $errorLevelMap = [], $exceptionLevelMap = [], $fatalLevel = null): self { $handler = new static($logger); if ($errorLevelMap !== false) { @@ -70,34 +71,42 @@ class ErrorHandler return $handler; } - public function registerExceptionHandler($levelMap = [], $callPrevious = true) + public function registerExceptionHandler($levelMap = [], $callPrevious = true): self { $prev = set_exception_handler([$this, 'handleException']); $this->uncaughtExceptionLevelMap = array_replace($this->defaultExceptionLevelMap(), $levelMap); if ($callPrevious && $prev) { $this->previousExceptionHandler = $prev; } + + return $this; } - public function registerErrorHandler(array $levelMap = [], $callPrevious = true, $errorTypes = -1) + public function registerErrorHandler(array $levelMap = [], $callPrevious = true, $errorTypes = -1, $handleOnlyReportedErrors = true): self { $prev = set_error_handler([$this, 'handleError'], $errorTypes); $this->errorLevelMap = array_replace($this->defaultErrorLevelMap(), $levelMap); if ($callPrevious) { $this->previousErrorHandler = $prev ?: true; } + + $this->handleOnlyReportedErrors = $handleOnlyReportedErrors; + + return $this; } - public function registerFatalHandler($level = null, $reservedMemorySize = 20) + public function registerFatalHandler($level = null, $reservedMemorySize = 20): self { register_shutdown_function([$this, 'handleFatalError']); $this->reservedMemory = str_repeat(' ', 1024 * $reservedMemorySize); $this->fatalLevel = $level; $this->hasFatalErrorHandler = true; + + return $this; } - protected function defaultExceptionLevelMap() + protected function defaultExceptionLevelMap(): array { return [ 'ParseError' => LogLevel::CRITICAL, @@ -105,7 +114,7 @@ class ErrorHandler ]; } - protected function defaultErrorLevelMap() + protected function defaultErrorLevelMap(): array { return [ E_ERROR => LogLevel::CRITICAL, @@ -157,14 +166,14 @@ class ErrorHandler */ public function handleError($code, $message, $file = '', $line = 0, $context = []) { - if (!(error_reporting() & $code)) { + if ($this->handleOnlyReportedErrors && !(error_reporting() & $code)) { return; } // fatal error codes are ignored if a fatal error handler is present as well to avoid duplicate log entries if (!$this->hasFatalErrorHandler || !in_array($code, self::$fatalErrors, true)) { $level = isset($this->errorLevelMap[$code]) ? $this->errorLevelMap[$code] : LogLevel::CRITICAL; - $this->logger->log($level, self::codeToString($code).': '.$message, ['code' => $code, 'message' => $message, 'file' => $file, 'line' => $line, 'context' => $context]); + $this->logger->log($level, self::codeToString($code).': '.$message, ['code' => $code, 'message' => $message, 'file' => $file, 'line' => $line]); } if ($this->previousErrorHandler === true) { @@ -179,7 +188,7 @@ class ErrorHandler */ public function handleFatalError() { - $this->reservedMemory = null; + $this->reservedMemory = ''; $lastError = error_get_last(); if ($lastError && in_array($lastError['type'], self::$fatalErrors, true)) { @@ -197,7 +206,7 @@ class ErrorHandler } } - private static function codeToString($code) + private static function codeToString($code): string { switch ($code) { case E_ERROR: |