Skip to content

Commit

Permalink
Merge pull request #997 from spiral/bugfix/monolog-bubble
Browse files Browse the repository at this point in the history
[spiral/monolog-bridge] Set `bubble` as `true` by default in logRotate method
  • Loading branch information
butschster authored Sep 20, 2023
2 parents 90b95aa + ac5717f commit 81d9669
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Bridge/Monolog/src/Bootloader/MonologBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function logRotate(
string $filename,
int $level = Logger::DEBUG,
int $maxFiles = 0,
bool $bubble = false
bool $bubble = true
): HandlerInterface {
$handler = new RotatingFileHandler(
$filename,
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Monolog/src/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public function handle(array $record): bool
$listener($e);
}

return true;
return false === $this->bubble;
}
}
69 changes: 69 additions & 0 deletions src/Bridge/Monolog/tests/EventHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Spiral\Tests\Monolog;

use PHPUnit\Framework\TestCase;
use Spiral\Logger\Event\LogEvent;
use Spiral\Logger\ListenerRegistry;
use Spiral\Monolog\EventHandler;

final class EventHandlerTest extends TestCase
{
private ListenerRegistry $registry;
private object $listener;

protected function setUp(): void
{
$this->listener = new class() {
public LogEvent $event;

public function __invoke(LogEvent $event): void
{
$this->event = $event;
}
};

$this->registry = new ListenerRegistry();
$this->registry->addListener($this->listener);
}

public function testHandle(): void
{
$handler = new EventHandler($this->registry);

$result = $handler->handle([
'datetime' => new \DateTimeImmutable(),
'channel' => 'foo',
'level' => 100,
'message' => 'bar',
'context' => ['foo' => 'bar'],
]);

$this->assertSame('foo', $this->listener->event->getChannel());
$this->assertSame('bar', $this->listener->event->getMessage());
$this->assertSame(['foo' => 'bar'], $this->listener->event->getContext());
$this->assertSame('debug', $this->listener->event->getLevel());
$this->assertFalse($result);
}

public function testHandleWithBubbleFalse(): void
{
$handler = new EventHandler(listenerRegistry: $this->registry, bubble: false);

$result = $handler->handle([
'datetime' => new \DateTimeImmutable(),
'channel' => 'foo',
'level' => 100,
'message' => 'bar',
'context' => ['foo' => 'bar'],
]);

$this->assertSame('foo', $this->listener->event->getChannel());
$this->assertSame('bar', $this->listener->event->getMessage());
$this->assertSame(['foo' => 'bar'], $this->listener->event->getContext());
$this->assertSame('debug', $this->listener->event->getLevel());
$this->assertTrue($result);
}
}
1 change: 1 addition & 0 deletions src/Bridge/Monolog/tests/RotateHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function load(string $section): array
$this->assertInstanceOf(RotatingFileHandler::class, $handler);

$this->assertSame(Logger::DEBUG, $handler->getLevel());
$this->assertTrue($handler->getBubble());
}

public function testChangeFormat(): void
Expand Down

0 comments on commit 81d9669

Please sign in to comment.