diff --git a/composer.json b/composer.json index 3780188e..37ddf0f9 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,7 @@ "dflydev/dot-access-data": "^3.0", "php-debugbar/php-debugbar": "^2.2", "phpmailer/phpmailer": "^7.1", - "psr/log": "^2.0", + "psr/log": "^2.0 || ^3.0", "rakibtg/sleekdb": "^2.13", "swagger-api/swagger-ui": "^5.32", "zircote/swagger-php": "^5.8", diff --git a/src/Logger/Logger.php b/src/Logger/Logger.php index deb91fdf..b94f6a52 100644 --- a/src/Logger/Logger.php +++ b/src/Logger/Logger.php @@ -13,6 +13,7 @@ use Quantum\Logger\Contracts\ReportableInterface; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; +use Stringable; /** * Class Logger @@ -35,7 +36,7 @@ public function getAdapter(): ReportableInterface /** * @inheritDoc */ - public function emergency($message, array $context = []): void + public function emergency(string|Stringable $message, array $context = []): void { $this->log(LogLevel::EMERGENCY, $message, $context); } @@ -43,7 +44,7 @@ public function emergency($message, array $context = []): void /** * @inheritDoc */ - public function alert($message, array $context = []): void + public function alert(string|Stringable $message, array $context = []): void { $this->log(LogLevel::ALERT, $message, $context); } @@ -51,7 +52,7 @@ public function alert($message, array $context = []): void /** * @inheritDoc */ - public function critical($message, array $context = []): void + public function critical(string|Stringable $message, array $context = []): void { $this->log(LogLevel::CRITICAL, $message, $context); } @@ -59,7 +60,7 @@ public function critical($message, array $context = []): void /** * @inheritDoc */ - public function error($message, array $context = []): void + public function error(string|Stringable $message, array $context = []): void { $this->log(LogLevel::ERROR, $message, $context); } @@ -67,7 +68,7 @@ public function error($message, array $context = []): void /** * @inheritDoc */ - public function warning($message, array $context = []): void + public function warning(string|Stringable $message, array $context = []): void { $this->log(LogLevel::WARNING, $message, $context); } @@ -75,7 +76,7 @@ public function warning($message, array $context = []): void /** * @inheritDoc */ - public function notice($message, array $context = []): void + public function notice(string|Stringable $message, array $context = []): void { $this->log(LogLevel::NOTICE, $message, $context); } @@ -83,7 +84,7 @@ public function notice($message, array $context = []): void /** * @inheritDoc */ - public function info($message, array $context = []): void + public function info(string|Stringable $message, array $context = []): void { $this->log(LogLevel::INFO, $message, $context); } @@ -91,7 +92,7 @@ public function info($message, array $context = []): void /** * @inheritDoc */ - public function debug($message, array $context = []): void + public function debug(string|Stringable $message, array $context = []): void { $this->log(LogLevel::DEBUG, $message, $context); } @@ -99,7 +100,7 @@ public function debug($message, array $context = []): void /** * @inheritDoc */ - public function log($level, $message, array $context = []): void + public function log($level, string|Stringable $message, array $context = []): void { if (is_debug_mode() || LoggerConfig::getLogLevel($level) >= LoggerConfig::getAppLogLevel()) { $this->adapter->report($level, $message, $context); diff --git a/tests/Unit/Logger/LoggerTest.php b/tests/Unit/Logger/LoggerTest.php index d88542d9..aa40beab 100644 --- a/tests/Unit/Logger/LoggerTest.php +++ b/tests/Unit/Logger/LoggerTest.php @@ -6,6 +6,8 @@ use Quantum\Logger\Adapters\MessageAdapter; use Quantum\Tests\Unit\AppTestCase; use Quantum\Logger\Logger; +use Psr\Log\LogLevel; +use Stringable; class LoggerTest extends AppTestCase { @@ -40,4 +42,45 @@ public function testLoggerLogReportsTheMessageToTheAdapter(): void $this->assertSame([['info' => 'Test message'], ['error' => 'Error message']], $this->adapter->getMessages()); } + + /** + * @dataProvider levelMethodProvider + */ + public function testLoggerLevelMethodReportsTheMessageWithItsLevel(string $method, string $level): void + { + $this->logger->$method('Some message'); + + $this->assertSame([[$level => 'Some message']], $this->adapter->getMessages()); + } + + public function testLoggerAcceptsAStringableMessage(): void + { + $message = new class () implements Stringable { + public function __toString(): string + { + return 'From an object'; + } + }; + + $this->logger->critical($message); + + $this->assertSame([['critical' => $message]], $this->adapter->getMessages()); + } + + /** + * @return array + */ + public function levelMethodProvider(): array + { + return [ + 'emergency' => ['emergency', LogLevel::EMERGENCY], + 'alert' => ['alert', LogLevel::ALERT], + 'critical' => ['critical', LogLevel::CRITICAL], + 'error' => ['error', LogLevel::ERROR], + 'warning' => ['warning', LogLevel::WARNING], + 'notice' => ['notice', LogLevel::NOTICE], + 'info' => ['info', LogLevel::INFO], + 'debug' => ['debug', LogLevel::DEBUG], + ]; + } }