<?php
declare(strict_types=1);
namespace App\Infrastructure\EventSubscriber;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class RequestLoggerSubscriber implements EventSubscriberInterface
{
private LoggerInterface $logger;
public function __construct(LoggerInterface $requestsLogger)
{
$this->logger = $requestsLogger;
}
public function onKernelRequest(RequestEvent $event)
{
if ($_ENV['APP_ENV'] === 'dev') {
$request = $event->getRequest();
$this->logger->info('Incoming request', [
'method' => $request->getMethod(),
'path' => $request->getPathInfo(),
'client_ip' => $request->getClientIp(),
'query_params' => $request->query->all(),
'request_headers' => $request->headers->all(),
'body' => $request->getContent(),
]);
}
}
public static function getSubscribedEvents(): array
{
// Listen to the Kernel Request event with the highest priority
return [
KernelEvents::REQUEST => ['onKernelRequest', 10000],
];
}
}