From 837f437b4c07022cb454bef1ad5a3a50b7dcc531 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 9 Aug 2015 18:16:16 +0100 Subject: [PATCH] Avoid logging fatal errors twice when both error and fatal error handlers are present, fixes #622 --- src/Monolog/ErrorHandler.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Monolog/ErrorHandler.php b/src/Monolog/ErrorHandler.php index 4c55c023..2a1c2c59 100644 --- a/src/Monolog/ErrorHandler.php +++ b/src/Monolog/ErrorHandler.php @@ -34,6 +34,7 @@ class ErrorHandler private $previousErrorHandler; private $errorLevelMap; + private $hasFatalErrorHandler; private $fatalLevel; private $reservedMemory; private static $fatalErrors = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR); @@ -94,6 +95,7 @@ class ErrorHandler $this->reservedMemory = str_repeat(' ', 1024 * $reservedMemorySize); $this->fatalLevel = $level; + $this->hasFatalErrorHandler = true; } protected function defaultErrorLevelMap() @@ -144,8 +146,11 @@ class ErrorHandler return; } - $level = isset($this->errorLevelMap[$code]) ? $this->errorLevelMap[$code] : LogLevel::CRITICAL; - $this->logger->log($level, self::codeToString($code).': '.$message, array('code' => $code, 'message' => $message, 'file' => $file, 'line' => $line)); + // 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, array('code' => $code, 'message' => $message, 'file' => $file, 'line' => $line)); + } if ($this->previousErrorHandler === true) { return false; @@ -162,7 +167,7 @@ class ErrorHandler $this->reservedMemory = null; $lastError = error_get_last(); - if ($lastError && in_array($lastError['type'], self::$fatalErrors)) { + if ($lastError && in_array($lastError['type'], self::$fatalErrors, true)) { $this->logger->log( $this->fatalLevel === null ? LogLevel::ALERT : $this->fatalLevel, 'Fatal Error ('.self::codeToString($lastError['type']).'): '.$lastError['message'],