vendor/monolog/monolog/src/Monolog/Processor/PsrLogMessageProcessor.php line 24

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * This file is part of the Monolog package.
  4.  *
  5.  * (c) Jordi Boggiano <[email protected]>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Monolog\Processor;
  11. use Monolog\Utils;
  12. use Monolog\LogRecord;
  13. /**
  14.  * Processes a record's message according to PSR-3 rules
  15.  *
  16.  * It replaces {foo} with the value from $context['foo']
  17.  *
  18.  * @author Jordi Boggiano <[email protected]>
  19.  */
  20. class PsrLogMessageProcessor implements ProcessorInterface
  21. {
  22.     public const SIMPLE_DATE "Y-m-d\TH:i:s.uP";
  23.     private ?string $dateFormat;
  24.     private bool $removeUsedContextFields;
  25.     /**
  26.      * @param string|null $dateFormat              The format of the timestamp: one supported by DateTime::format
  27.      * @param bool        $removeUsedContextFields If set to true the fields interpolated into message gets unset
  28.      */
  29.     public function __construct(?string $dateFormat nullbool $removeUsedContextFields false)
  30.     {
  31.         $this->dateFormat $dateFormat;
  32.         $this->removeUsedContextFields $removeUsedContextFields;
  33.     }
  34.     /**
  35.      * @inheritDoc
  36.      */
  37.     public function __invoke(LogRecord $record): LogRecord
  38.     {
  39.         if (false === strpos($record->message'{')) {
  40.             return $record;
  41.         }
  42.         $replacements = [];
  43.         $context $record->context;
  44.         foreach ($context as $key => $val) {
  45.             $placeholder '{' $key '}';
  46.             if (strpos($record->message$placeholder) === false) {
  47.                 continue;
  48.             }
  49.             if (null === $val || \is_scalar($val) || (\is_object($val) && method_exists($val"__toString"))) {
  50.                 $replacements[$placeholder] = $val;
  51.             } elseif ($val instanceof \DateTimeInterface) {
  52.                 if (null === $this->dateFormat && $val instanceof \Monolog\JsonSerializableDateTimeImmutable) {
  53.                     // handle monolog dates using __toString if no specific dateFormat was asked for
  54.                     // so that it follows the useMicroseconds flag
  55.                     $replacements[$placeholder] = (string) $val;
  56.                 } else {
  57.                     $replacements[$placeholder] = $val->format($this->dateFormat ?? static::SIMPLE_DATE);
  58.                 }
  59.             } elseif ($val instanceof \UnitEnum) {
  60.                 $replacements[$placeholder] = $val instanceof \BackedEnum $val->value $val->name;
  61.             } elseif (\is_object($val)) {
  62.                 $replacements[$placeholder] = '[object '.Utils::getClass($val).']';
  63.             } elseif (\is_array($val)) {
  64.                 $replacements[$placeholder] = 'array'.Utils::jsonEncode($valnulltrue);
  65.             } else {
  66.                 $replacements[$placeholder] = '['.\gettype($val).']';
  67.             }
  68.             if ($this->removeUsedContextFields) {
  69.                 unset($context[$key]);
  70.             }
  71.         }
  72.         return $record->with(messagestrtr($record->message$replacements), context$context);
  73.     }
  74. }