Skip to content

Commit

Permalink
Merge pull request #98 from spiral/monolog-v3
Browse files Browse the repository at this point in the history
Add support for monolog/monolog 3.x
  • Loading branch information
butschster committed Jan 3, 2024
2 parents fd53f42 + 81450a3 commit 15013b3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/Logger/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Handler\HandlerInterface;
use Monolog\Level;
use Monolog\Logger;
use Monolog\LogRecord;
use RoadRunner\Logger\Logger as RoadRunnerLogger;

final class Handler extends AbstractProcessingHandler
Expand All @@ -29,7 +31,7 @@ public function __construct(
$this->setFormatter($formatter);
}

public function handle(array $record): bool
public function handle(array|LogRecord $record): bool
{
if ($this->fallbackHandler !== null) {
return $this->fallbackHandler->handle($record);
Expand All @@ -38,11 +40,16 @@ public function handle(array $record): bool
return parent::handle($record);
}

protected function write(array $record): void
protected function write(array|LogRecord $record): void
{
/** @psalm-suppress InvalidArgument */
$message = $record['formatted'];
\assert(\is_string($message) || $message instanceof \Stringable);

match ($record['level']) {
$level = $record['level'] instanceof Level ? $record['level']->value : $record['level'];

/** @psalm-suppress DeprecatedConstant */
match ($level) {
Logger::ERROR, Logger::CRITICAL => $this->logger->error($message),
Logger::WARNING, Logger::ALERT, Logger::EMERGENCY => $this->logger->warning($message),
Logger::INFO, Logger::NOTICE => $this->logger->info($message),
Expand Down
3 changes: 2 additions & 1 deletion tests/src/Logger/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Spiral\Tests\Logger;

use Monolog\LogRecord;
use RoadRunner\Logger\Logger;
use Spiral\Goridge\RPC\RPCInterface;
use Monolog\Handler\HandlerInterface;
Expand Down Expand Up @@ -54,7 +55,7 @@ public function testLoggerShouldSendDataToFallback(): void
),
]);

$fallback->shouldReceive('handle')->withArgs(function (array $record) {
$fallback->shouldReceive('handle')->withArgs(function (array|LogRecord $record) {
return $record['message'] === 'Error message';
})->andReturn(true);

Expand Down

0 comments on commit 15013b3

Please sign in to comment.