Skip to content

Commit

Permalink
Merge pull request #106 from scoutapp/105-clear-error-message-for-inv…
Browse files Browse the repository at this point in the history
…alid-log-level

Add a clearer exception description for invalid log level
  • Loading branch information
Chris Schneider authored Nov 1, 2019
2 parents 4b6fd86 + 6f38b79 commit 1b0d4cb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion src/Logger/FilteredLogLevelDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/Logger/FilteredLogLevelDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1b0d4cb

Please sign in to comment.