mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-29 11:26:09 +01: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 FormatterInterface|null $formatter = null;
|
||||
private bool $includeExtra;
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
$this->logger = $logger;
|
||||
$this->includeExtra = $includeExtra;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,12 +55,15 @@ class PsrHandler extends AbstractHandler implements FormattableHandlerInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->formatter !== null) {
|
||||
$formatted = $this->formatter->format($record);
|
||||
$this->logger->log($record->level->toPsrLogLevel(), (string) $formatted, $record->context);
|
||||
} else {
|
||||
$this->logger->log($record->level->toPsrLogLevel(), $record->message, $record->context);
|
||||
}
|
||||
$message = $this->formatter !== null
|
||||
? (string) $this->formatter->format($record)
|
||||
: $record->message;
|
||||
|
||||
$context = $this->includeExtra
|
||||
? [...$record->extra, ...$record->context]
|
||||
: $record->context;
|
||||
|
||||
$this->logger->log($record->level->toPsrLogLevel(), $message, $context);
|
||||
|
||||
return false === $this->bubble;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user