src/EventSubscriber/LoggerSubscriber.php line 105

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Psr\Log\LoggerInterface;
  4. use Symfony\Component\Console\ConsoleEvents;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  8. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  9. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  10. use Symfony\Component\Console\Event\ConsoleErrorEvent;
  11. use Symfony\Component\Console\Event\ConsoleTerminateEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use App\Utils\Logging\Logger\CustomJsonFormatter as Formatter;
  15. class LoggerSubscriber implements EventSubscriberInterface
  16. {
  17.     public const SHORT_MESSAGE_CORE_API_RESPONSE 'CORE API RESPONSE';
  18.     public const SHORT_MESSAGE_CORE_API_REQUEST 'CORE API INPUT';
  19.     public const SHORT_MESSAGE_CORE_API_ERROR 'CORE API ERROR';
  20.     public const DIRECTION_CORE_O 'CORE-O';
  21.     public const DIRECTION_CORE_I 'CORE-I';
  22.     private $logger;
  23.     private $isExitWithError;
  24.     public function __construct(LoggerInterface $logger)
  25.     {
  26.         $this->logger $logger;
  27.         $this->isExitWithError false;
  28.     }
  29.     public function onConsoleError(ConsoleErrorEvent $event): void
  30.     {
  31.         if (!$event->getError()) {
  32.             return;
  33.         }
  34.         $command $event->getCommand();
  35.         $this->isExitWithError true;
  36.         $this->logger->error(sprintf('Command: %s execution error: %s'$command $command->getName() : 'null'$event->getError()->getMessage()), [
  37.             Formatter::MSG_SUBJECT => $event->getError(),
  38.             Formatter::TYPE => Formatter::TYPE_COMMAND,
  39.         ]);
  40.     }
  41.     public function onConsoleTerminate(ConsoleTerminateEvent $event): void
  42.     {
  43.         if ($this->isExitWithError) {
  44.             return;
  45.         }
  46.         $command $event->getCommand();
  47.         $this->logger->info('Command end: ' . ($command $command->getName() : 'null'), [
  48.             Formatter::MSG_SUBJECT => ['EXIT_CODE' => $event->getExitCode()],
  49.             Formatter::TYPE => Formatter::TYPE_COMMAND,
  50.         ]);
  51.     }
  52.     public function onCommand(ConsoleCommandEvent $event): void
  53.     {
  54.         $command $event->getCommand();
  55.         $this->logger->info('Command start: ' . ($command $command->getName() : 'null'), [
  56.             Formatter::MSG_SUBJECT => $event->getInput()->getArguments(),
  57.             Formatter::TYPE => Formatter::TYPE_COMMAND,
  58.         ]);
  59.     }
  60.     public function onKernelRequest(RequestEvent $event): void
  61.     {
  62.         if ($this->isExitWithError) {
  63.             return;
  64.         }
  65.         $this->logger->info(self::SHORT_MESSAGE_CORE_API_REQUEST, [
  66.             Formatter::MSG_SUBJECT => $event->getRequest(),
  67.             Formatter::MSG_DIRECTION => self::DIRECTION_CORE_I,
  68.             Formatter::TYPE => Formatter::TYPE_REQUEST,
  69.         ]);
  70.     }
  71.     public function onKernelResponse(ResponseEvent $event): void
  72.     {
  73.         if ($this->isExitWithError) {
  74.             return;
  75.         }
  76.         if ($event->getResponse() instanceof JsonResponse) {
  77.             $this->logger->info(self::SHORT_MESSAGE_CORE_API_RESPONSE, [
  78.                 Formatter::MSG_SUBJECT => json_decode($event->getResponse()->getContent() ?? ''true),
  79.                 Formatter::MSG_STATUS_CODE => $event->getResponse()->getStatusCode(),
  80.                 Formatter::MSG_DIRECTION => self::DIRECTION_CORE_O,
  81.                 Formatter::TYPE => Formatter::TYPE_RESPONSE,
  82.             ]);
  83.             return;
  84.         }
  85.         $this->logger->info(self::SHORT_MESSAGE_CORE_API_RESPONSE, [
  86.             Formatter::MSG_SUBJECT => $event->getResponse()->getContent(),
  87.             Formatter::MSG_STATUS_CODE => $event->getResponse()->getStatusCode(),
  88.             Formatter::MSG_DIRECTION => self::DIRECTION_CORE_O,
  89.             Formatter::TYPE => Formatter::TYPE_RESPONSE,
  90.         ]);
  91.     }
  92.     public function onKernelException(ExceptionEvent $event): void
  93.     {
  94.         $this->isExitWithError true;
  95.         $this->logger->error(self::SHORT_MESSAGE_CORE_API_ERROR, [
  96.             Formatter::MSG_SUBJECT => $event->getThrowable(),
  97.             Formatter::TYPE => Formatter::TYPE_RESPONSE,
  98.         ]);
  99.     }
  100.     public static function getSubscribedEvents(): array
  101.     {
  102.         return [
  103.             KernelEvents::REQUEST => ['onKernelRequest'7],
  104.             KernelEvents::RESPONSE => ['onKernelResponse'1],
  105.             KernelEvents::EXCEPTION => 'onKernelException',
  106.             ConsoleEvents::ERROR => 'onConsoleError',
  107.             ConsoleEvents::TERMINATE => 'onConsoleTerminate',
  108.             ConsoleEvents::COMMAND => 'onCommand',
  109.         ];
  110.     }
  111. }