1
0
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:
Witold Wasiczko
2024-04-12 11:00:56 +02:00
committed by GitHub
parent 000fedbcf2
commit 35dab43e3c
2 changed files with 28 additions and 7 deletions

View File

@@ -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;
}