<?php
declare(strict_types=1);
namespace Comalia\GesicaBundle\Logger;
use Monolog\Processor\ProcessorInterface;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
/**
* Class CorrelationIdProcessor
*
* Logger processor pour ajouter le correlation ID aux logs
*/
class CorrelationIdProcessor implements ProcessorInterface
{
/**
* @var RequestStack
*/
private $requestStack;
/**
* CorrelationIdProcessor constructor.
*
* @param RequestStack $requestStack
*/
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
/**
* @param array $record
* @return array
*/
public function __invoke(array $record): array
{
$session = $this->getSession();
if ($session) {
$record['extra']['correlation_id'] = $session->get('x-correlation-id');
}
return $record;
}
/**
* @return SessionInterface
*/
private function getSession(): ?SessionInterface
{
try {
return $this->requestStack->getSession();
} catch (SessionNotFoundException $ex) {
return null;
}
}
}