1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-02 11:20:31 +02:00

Fix level map override when using setExceptionHandler, fixes #1005

This commit is contained in:
Jordi Boggiano
2017-06-19 03:08:32 +02:00
parent 443e4a94fc
commit 69b21064ca
2 changed files with 11 additions and 20 deletions

View File

@@ -77,7 +77,12 @@ class ErrorHandler
public function registerExceptionHandler($levelMap = [], $callPrevious = true): self public function registerExceptionHandler($levelMap = [], $callPrevious = true): self
{ {
$prev = set_exception_handler([$this, 'handleException']); $prev = set_exception_handler([$this, 'handleException']);
$this->uncaughtExceptionLevelMap = array_replace($this->defaultExceptionLevelMap(), $levelMap); $this->uncaughtExceptionLevelMap = $levelMap;
foreach ($this->defaultExceptionLevelMap() as $class => $level) {
if (!isset($this->uncaughtExceptionLevelMap[$class])) {
$this->uncaughtExceptionLevelMap[$class] = $level;
}
}
if ($callPrevious && $prev) { if ($callPrevious && $prev) {
$this->previousExceptionHandler = $prev; $this->previousExceptionHandler = $prev;
} }

View File

@@ -12,6 +12,7 @@
namespace Monolog; namespace Monolog;
use Monolog\Handler\TestHandler; use Monolog\Handler\TestHandler;
use Psr\Log\LogLevel;
class ErrorHandlerTest extends \PHPUnit\Framework\TestCase class ErrorHandlerTest extends \PHPUnit\Framework\TestCase
{ {
@@ -82,27 +83,12 @@ class ErrorHandlerTest extends \PHPUnit\Framework\TestCase
$logger = new Logger('test', [$handler = new TestHandler]); $logger = new Logger('test', [$handler = new TestHandler]);
$errHandler = new ErrorHandler($logger); $errHandler = new ErrorHandler($logger);
$resHandler = $errHandler->registerExceptionHandler(['Monolog\CustomTestException' => Logger::ALERT, 'Throwable' => Logger::WARNING], false); $resHandler = $errHandler->registerExceptionHandler($map = ['Monolog\CustomTestException' => LogLevel::DEBUG, 'TypeError' => LogLevel::NOTICE, 'Throwable' => LogLevel::WARNING], false);
$this->assertSame($errHandler, $resHandler); $this->assertSame($errHandler, $resHandler);
try { $map['ParseError'] = LogLevel::CRITICAL;
throw new CustomCustomException(); $prop = $this->getPrivatePropertyValue($errHandler, 'uncaughtExceptionLevelMap');
$this->assertCount(1, $handler->getRecords()); $this->assertSame($map, $prop);
$this->assertTrue($handler->hasAlertRecords());
} catch (\Throwable $e) {
}
try {
throw new CustomTestException();
$this->assertCount(2, $handler->getRecords());
$this->assertTrue($handler->hasAlertRecords());
} catch (\Throwable $e) {
}
try {
throw new RuntimeException();
$this->assertCount(3, $handler->getRecords());
$this->assertTrue($handler->hasWarningRecords());
} catch (\Throwable $e) {
}
$errHandler->registerExceptionHandler([], true); $errHandler->registerExceptionHandler([], true);
$prop = $this->getPrivatePropertyValue($errHandler, 'previousExceptionHandler'); $prop = $this->getPrivatePropertyValue($errHandler, 'previousExceptionHandler');