src/EventSubscriber/PostResponseLogSubscriber.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Psr\Log\LoggerInterface;
  7. class PostResponseLogSubscriber implements EventSubscriberInterface
  8. {
  9.     private LoggerInterface $logger;
  10.     public function __construct(LoggerInterface $logger)
  11.     {
  12.         $this->logger $logger;
  13.     }
  14.     public function onKernelTerminate(TerminateEvent $event): void
  15.     {
  16.         $request $event->getRequest();
  17.         $response $event->getResponse();
  18.         // Log the data
  19.         $this->logger->info('Request handled', [
  20.             'method' => $request->getMethod(),
  21.             'path' => $request->getPathInfo(),
  22.             'status_code' => $response->getStatusCode(),
  23.         ]);
  24.         // Save additional logs to a database or file if needed
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             KernelEvents::TERMINATE => 'onKernelTerminate',
  30.         ];
  31.     }
  32. }