mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-01 10:50:21 +02:00
Include extra in context in PsrHandler (#1852)
This commit is contained in:
@@ -33,15 +33,17 @@ class PsrHandler extends AbstractHandler implements FormattableHandlerInterface
|
|||||||
protected LoggerInterface $logger;
|
protected LoggerInterface $logger;
|
||||||
|
|
||||||
protected FormatterInterface|null $formatter = null;
|
protected FormatterInterface|null $formatter = null;
|
||||||
|
private bool $includeExtra;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param LoggerInterface $logger The underlying PSR-3 compliant logger to which messages will be proxied
|
* @param LoggerInterface $logger The underlying PSR-3 compliant logger to which messages will be proxied
|
||||||
*/
|
*/
|
||||||
public function __construct(LoggerInterface $logger, int|string|Level $level = Level::Debug, bool $bubble = true)
|
public function __construct(LoggerInterface $logger, int|string|Level $level = Level::Debug, bool $bubble = true, bool $includeExtra = false)
|
||||||
{
|
{
|
||||||
parent::__construct($level, $bubble);
|
parent::__construct($level, $bubble);
|
||||||
|
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
|
$this->includeExtra = $includeExtra;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -53,12 +55,15 @@ class PsrHandler extends AbstractHandler implements FormattableHandlerInterface
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->formatter !== null) {
|
$message = $this->formatter !== null
|
||||||
$formatted = $this->formatter->format($record);
|
? (string) $this->formatter->format($record)
|
||||||
$this->logger->log($record->level->toPsrLogLevel(), (string) $formatted, $record->context);
|
: $record->message;
|
||||||
} else {
|
|
||||||
$this->logger->log($record->level->toPsrLogLevel(), $record->message, $record->context);
|
$context = $this->includeExtra
|
||||||
}
|
? [...$record->extra, ...$record->context]
|
||||||
|
: $record->context;
|
||||||
|
|
||||||
|
$this->logger->log($record->level->toPsrLogLevel(), $message, $context);
|
||||||
|
|
||||||
return false === $this->bubble;
|
return false === $this->bubble;
|
||||||
}
|
}
|
||||||
|
@@ -60,4 +60,20 @@ class PsrHandlerTest extends TestCase
|
|||||||
$handler->setFormatter(new LineFormatter('dummy'));
|
$handler->setFormatter(new LineFormatter('dummy'));
|
||||||
$handler->handle($this->getRecord($level, $message, context: $context, datetime: new \DateTimeImmutable()));
|
$handler->handle($this->getRecord($level, $message, context: $context, datetime: new \DateTimeImmutable()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testIncludeExtra()
|
||||||
|
{
|
||||||
|
$message = 'Hello, world!';
|
||||||
|
$context = ['foo' => 'bar'];
|
||||||
|
$extra = ['baz' => 'boo'];
|
||||||
|
$level = Level::Error;
|
||||||
|
|
||||||
|
$psrLogger = $this->createMock('Psr\Log\NullLogger');
|
||||||
|
$psrLogger->expects($this->once())
|
||||||
|
->method('log')
|
||||||
|
->with($level->toPsrLogLevel(), $message, ['baz' => 'boo', 'foo' => 'bar']);
|
||||||
|
|
||||||
|
$handler = new PsrHandler($psrLogger, includeExtra: true);
|
||||||
|
$handler->handle($this->getRecord($level, $message, context: $context, datetime: new \DateTimeImmutable(), extra: $extra));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user