Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 10 additions & 9 deletions src/Logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Quantum\Logger\Contracts\ReportableInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Stringable;

/**
* Class Logger
Expand All @@ -35,71 +36,71 @@ 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);
}

/**
* @inheritDoc
*/
public function alert($message, array $context = []): void
public function alert(string|Stringable $message, array $context = []): void
{
$this->log(LogLevel::ALERT, $message, $context);
}

/**
* @inheritDoc
*/
public function critical($message, array $context = []): void
public function critical(string|Stringable $message, array $context = []): void
{
$this->log(LogLevel::CRITICAL, $message, $context);
}

/**
* @inheritDoc
*/
public function error($message, array $context = []): void
public function error(string|Stringable $message, array $context = []): void
{
$this->log(LogLevel::ERROR, $message, $context);
}

/**
* @inheritDoc
*/
public function warning($message, array $context = []): void
public function warning(string|Stringable $message, array $context = []): void
{
$this->log(LogLevel::WARNING, $message, $context);
}

/**
* @inheritDoc
*/
public function notice($message, array $context = []): void
public function notice(string|Stringable $message, array $context = []): void
{
$this->log(LogLevel::NOTICE, $message, $context);
}

/**
* @inheritDoc
*/
public function info($message, array $context = []): void
public function info(string|Stringable $message, array $context = []): void
{
$this->log(LogLevel::INFO, $message, $context);
}

/**
* @inheritDoc
*/
public function debug($message, array $context = []): void
public function debug(string|Stringable $message, array $context = []): void
{
$this->log(LogLevel::DEBUG, $message, $context);
}

/**
* @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);
Expand Down
43 changes: 43 additions & 0 deletions tests/Unit/Logger/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<string, array{0: string, 1: string}>
*/
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],
];
}
}
Loading