From 5d98295968023210dc0e8f9ef36ca2612e08506f Mon Sep 17 00:00:00 2001 From: James Titcumb Date: Wed, 23 Oct 2019 15:31:24 -0400 Subject: [PATCH 1/2] Add a clearer exception description when an invalid log level is given to FilteredLogLevelDecorator --- src/Logger/FilteredLogLevelDecorator.php | 13 ++++++++++++- tests/Unit/Logger/FilteredLogLevelDecoratorTest.php | 10 ++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) 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); From 6f38b79a16b4382a1a72af215515475698f4798f Mon Sep 17 00:00:00 2001 From: James Titcumb Date: Wed, 23 Oct 2019 15:34:33 -0400 Subject: [PATCH 2/2] Updated changelog for #106 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6734a5fa..874b5032 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Pending +### Changed + + - Improved error message when invalid log level given for `log_level` configuration (#106) + ## [0.2.3] 2019-10-22 ### Added