media-cloud-sync
/
includes
/
sdk
/
google
/
monolog
/
monolog
/
src
/
Monolog
/
Handler
/
PsrHandler.php
PsrHandler.php in Media Cloud Sync 1.4.2, at includes/sdk/google/monolog/monolog/src/Monolog/Handler/PsrHandler.php
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | /* |
| 5 | * This file is part of the Monolog package. |
| 6 | * |
| 7 | * (c) Jordi Boggiano <[email protected]> |
| 8 | * |
| 9 | * For the full copyright and license information, please view the LICENSE |
| 10 | * file that was distributed with this source code. |
| 11 | */ |
| 12 | namespace Dudlewebs\WPMCS\GCP\Monolog\Handler; |
| 13 | |
| 14 | use Dudlewebs\WPMCS\GCP\Monolog\Level; |
| 15 | use Dudlewebs\WPMCS\GCP\Psr\Log\LoggerInterface; |
| 16 | use Dudlewebs\WPMCS\GCP\Monolog\Formatter\FormatterInterface; |
| 17 | use Dudlewebs\WPMCS\GCP\Monolog\LogRecord; |
| 18 | /** |
| 19 | * Proxies log messages to an existing PSR-3 compliant logger. |
| 20 | * |
| 21 | * If a formatter is configured, the formatter's output MUST be a string and the |
| 22 | * formatted message will be fed to the wrapped PSR logger instead of the original |
| 23 | * log record's message. |
| 24 | * |
| 25 | * @author Michael Moussa <[email protected]> |
| 26 | */ |
| 27 | class PsrHandler extends AbstractHandler implements FormattableHandlerInterface |
| 28 | { |
| 29 | /** |
| 30 | * PSR-3 compliant logger |
| 31 | */ |
| 32 | protected LoggerInterface $logger; |
| 33 | protected FormatterInterface|null $formatter = null; |
| 34 | private bool $includeExtra; |
| 35 | /** |
| 36 | * @param LoggerInterface $logger The underlying PSR-3 compliant logger to which messages will be proxied |
| 37 | */ |
| 38 | public function __construct(LoggerInterface $logger, int|string|Level $level = Level::Debug, bool $bubble = \true, bool $includeExtra = \false) |
| 39 | { |
| 40 | parent::__construct($level, $bubble); |
| 41 | $this->logger = $logger; |
| 42 | $this->includeExtra = $includeExtra; |
| 43 | } |
| 44 | /** |
| 45 | * @inheritDoc |
| 46 | */ |
| 47 | public function handle(LogRecord $record) : bool |
| 48 | { |
| 49 | if (!$this->isHandling($record)) { |
| 50 | return \false; |
| 51 | } |
| 52 | $message = $this->formatter !== null ? (string) $this->formatter->format($record) : $record->message; |
| 53 | $context = $this->includeExtra ? [...$record->extra, ...$record->context] : $record->context; |
| 54 | $this->logger->log($record->level->toPsrLogLevel(), $message, $context); |
| 55 | return \false === $this->bubble; |
| 56 | } |
| 57 | /** |
| 58 | * Sets the formatter. |
| 59 | */ |
| 60 | public function setFormatter(FormatterInterface $formatter) : HandlerInterface |
| 61 | { |
| 62 | $this->formatter = $formatter; |
| 63 | return $this; |
| 64 | } |
| 65 | /** |
| 66 | * Gets the formatter. |
| 67 | */ |
| 68 | public function getFormatter() : FormatterInterface |
| 69 | { |
| 70 | if ($this->formatter === null) { |
| 71 | throw new \LogicException('No formatter has been set and this handler does not have a default formatter'); |
| 72 | } |
| 73 | return $this->formatter; |
| 74 | } |
| 75 | } |
| 76 |