diff --git a/src/Logger/FilteredLogLevelDecorator.php b/src/Logger/FilteredLogLevelDecorator.php index 5af4b62e..465e5fdc 100644 --- a/src/Logger/FilteredLogLevelDecorator.php +++ b/src/Logger/FilteredLogLevelDecorator.php @@ -8,6 +8,9 @@ use Psr\Log\LoggerTrait; use Psr\Log\LogLevel; use Webmozart\Assert\Assert; +use function array_keys; +use function implode; +use function sprintf; use function strtolower; /** @@ -41,7 +44,15 @@ final class FilteredLogLevelDecorator implements LoggerInterface */ public function __construct(LoggerInterface $realLogger, string $minimumLogLevel) { - Assert::keyExists(self::LOG_LEVEL_ORDER, strtolower($minimumLogLevel)); + Assert::keyExists( + self::LOG_LEVEL_ORDER, + strtolower($minimumLogLevel), + sprintf( + 'Log level %s was not a valid PSR-3 compatible log level. Should be one of: %s', + $minimumLogLevel, + implode(', ', array_keys(self::LOG_LEVEL_ORDER)) + ) + ); $this->minimumLogLevel = self::LOG_LEVEL_ORDER[strtolower($minimumLogLevel)]; $this->realLogger = $realLogger; diff --git a/tests/Unit/Logger/FilteredLogLevelDecoratorTest.php b/tests/Unit/Logger/FilteredLogLevelDecoratorTest.php index 5a8888b1..8be37fcf 100644 --- a/tests/Unit/Logger/FilteredLogLevelDecoratorTest.php +++ b/tests/Unit/Logger/FilteredLogLevelDecoratorTest.php @@ -28,6 +28,16 @@ public function setUp() : void $this->decoratedLogger = $this->createMock(LoggerInterface::class); } + public function testInvalidLogLevelStringGivesClearErrorMessage() : void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + 'Log level foo was not a valid PSR-3 compatible log level. ' + . 'Should be one of: debug, info, notice, warning, error, critical, alert, emergency' + ); + new FilteredLogLevelDecorator($this->decoratedLogger, 'foo'); + } + public function testLogMessagesHaveScoutTagPrepended() : void { $decorator = new FilteredLogLevelDecorator($this->decoratedLogger, LogLevel::DEBUG);