1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-06 13:16:39 +02:00

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

This commit is contained in:
Jordi Boggiano
2015-08-09 18:16:16 +01:00
parent cd3f9f1e50
commit 837f437b4c

View File

@@ -34,6 +34,7 @@ class ErrorHandler
private $previousErrorHandler; private $previousErrorHandler;
private $errorLevelMap; private $errorLevelMap;
private $hasFatalErrorHandler;
private $fatalLevel; private $fatalLevel;
private $reservedMemory; private $reservedMemory;
private static $fatalErrors = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR); 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->reservedMemory = str_repeat(' ', 1024 * $reservedMemorySize);
$this->fatalLevel = $level; $this->fatalLevel = $level;
$this->hasFatalErrorHandler = true;
} }
protected function defaultErrorLevelMap() protected function defaultErrorLevelMap()
@@ -144,8 +146,11 @@ class ErrorHandler
return; return;
} }
$level = isset($this->errorLevelMap[$code]) ? $this->errorLevelMap[$code] : LogLevel::CRITICAL; // fatal error codes are ignored if a fatal error handler is present as well to avoid duplicate log entries
$this->logger->log($level, self::codeToString($code).': '.$message, array('code' => $code, 'message' => $message, 'file' => $file, 'line' => $line)); 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) { if ($this->previousErrorHandler === true) {
return false; return false;
@@ -162,7 +167,7 @@ class ErrorHandler
$this->reservedMemory = null; $this->reservedMemory = null;
$lastError = error_get_last(); $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->logger->log(
$this->fatalLevel === null ? LogLevel::ALERT : $this->fatalLevel, $this->fatalLevel === null ? LogLevel::ALERT : $this->fatalLevel,
'Fatal Error ('.self::codeToString($lastError['type']).'): '.$lastError['message'], 'Fatal Error ('.self::codeToString($lastError['type']).'): '.$lastError['message'],