Browse Source

Avoid logging fatal errors twice when both error and fatal error handlers are present, fixes #622

Jordi Boggiano 10 years ago
parent
commit
837f437b4c
1 changed files with 8 additions and 3 deletions
  1. 8 3
      src/Monolog/ErrorHandler.php

+ 8 - 3
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'],