<?php
namespace App\EventListener;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
class LogRequestAndResponseEventListener {
private $logger;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
$this->logger->info('requestData', [
'route' => $request->getMethod() . ' ' . $request->getRequestUri(),
'request body' => $request->request->all(),
'headers' => $request->headers->all(),
]);
}
public function onKernelTerminate(TerminateEvent $event)
{
$request = $event->getRequest();
$response = $event->getResponse();
$this->logger->info('responseData', [
'route' => $request->getMethod() . ' ' . $request->getRequestUri(),
'status' => $response->getStatusCode(),
]);
}
}