Browse Source

Added errant throwable removal from context

main
Dustin Wilson 1 year ago
parent
commit
cb793f21f3
  1. 8
      lib/Logger.php
  2. 5
      tests/cases/TestLogger.php
  3. 2
      tests/cases/TestStreamHandler.php

8
lib/Logger.php

@ -156,7 +156,13 @@ class Logger implements LoggerInterface {
# is actually an Exception before using it as such, as it MAY contain
# anything.
// We're not doing interpolation :)
// Really shitty user experience to not trigger a warning when someone does
// something incorrectly, but okay...
foreach ($context as $k => $v) {
if (($v instanceof \Throwable && $k !== 'exception') || (!$v instanceof \Throwable && $k === 'exception')) {
unset($context[$k]);
}
}
foreach ($this->handlers as $h) {
$h($level, $this->channel, $message, $context);

5
tests/cases/TestLogger.php

@ -57,8 +57,9 @@ class TestLogger extends \PHPUnit\Framework\TestCase {
$regex = '/^' . (new \DateTimeImmutable())->format('M d') . ' \d{2}:\d{2}:\d{2} ook ' . strtoupper($levelName) . ' Ook!\n/';
$this->assertEquals(1, preg_match($regex, $o));
// Try it again with Level enum
$l->log(constant(sprintf('%s::%s', Level::class, ucfirst($levelName))), 'Ook!');
// Try it again with Level enum, and for shits and giggles test removal
// of errant throwables in context
$l->log(constant(sprintf('%s::%s', Level::class, ucfirst($levelName))), 'Ook!', [ 'ook' => new \Exception('ook'), 'exception' => 'ook', 'eek' => 'Eek!' ]);
rewind($s);
$o = stream_get_contents($s);
$this->assertEquals(1, preg_match($regex, $o));

2
tests/cases/TestStreamHandler.php

@ -17,7 +17,7 @@ use MensBeam\Logger\{
/** @covers \MensBeam\Logger\StreamHandler */
class TestStreamHandler extends ErrorHandlingTestCase {
class TestStreamHandler extends \PHPUnit\Framework\TestCase {
/** @dataProvider provideResourceTypesTests */
public function testResourceTypes(\Closure $closure): void {
$regex = '/^' . (new \DateTimeImmutable())->format('M d') . ' \d{2}:\d{2}:\d{2} ook ERROR Ook!\nEek!\n/';

Loading…
Cancel
Save