vendor/comalia/gesica_bundle/Logger/CorrelationIdProcessor.php line 43

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Comalia\GesicaBundle\Logger;
  4. use Monolog\Processor\ProcessorInterface;
  5. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. /**
  9.  * Class CorrelationIdProcessor
  10.  *
  11.  * Logger processor pour ajouter le correlation ID aux logs
  12.  */
  13. class CorrelationIdProcessor implements ProcessorInterface
  14. {
  15.     /**
  16.      * @var RequestStack
  17.      */
  18.     private $requestStack;
  19.     /**
  20.      * CorrelationIdProcessor constructor.
  21.      *
  22.      * @param RequestStack $requestStack
  23.      */
  24.     public function __construct(RequestStack $requestStack)
  25.     {
  26.         $this->requestStack $requestStack;
  27.     }
  28.     /**
  29.      * @param array $record
  30.      * @return array
  31.      */
  32.     public function __invoke(array $record): array
  33.     {
  34.         $session $this->getSession();
  35.         if ($session) {
  36.             $record['extra']['correlation_id'] = $session->get('x-correlation-id');
  37.         }
  38.         return $record;
  39.     }
  40.     /**
  41.      * @return SessionInterface
  42.      */
  43.     private function getSession(): ?SessionInterface
  44.     {
  45.         try {
  46.             return $this->requestStack->getSession();
  47.         } catch (SessionNotFoundException $ex) {
  48.             return null;
  49.         }
  50.     }
  51. }