vendor/overblog/graphql-bundle/src/EventListener/ErrorLoggerListener.php line 25

Open in your IDE?
  1. <?php
  2. namespace Overblog\GraphQLBundle\EventListener;
  3. use GraphQL\Error\UserError;
  4. use Overblog\GraphQLBundle\Error\UserWarning;
  5. use Overblog\GraphQLBundle\Event\ErrorFormattingEvent;
  6. use Psr\Log\LoggerInterface;
  7. use Psr\Log\LogLevel;
  8. use Psr\Log\NullLogger;
  9. final class ErrorLoggerListener
  10. {
  11.     const DEFAULT_LOGGER_SERVICE 'logger';
  12.     /** @var LoggerInterface */
  13.     private $logger;
  14.     public function __construct(
  15.         LoggerInterface $logger null
  16.     ) {
  17.         $this->logger null === $logger ? new NullLogger() : $logger;
  18.     }
  19.     public function onErrorFormatting(ErrorFormattingEvent $event)
  20.     {
  21.         $error $event->getError();
  22.         if ($error->getPrevious()) {
  23.             $exception $error->getPrevious();
  24.             if ($exception instanceof UserError) {
  25.                 if ($exception->getPrevious()) {
  26.                     $this->log($exception->getPrevious());
  27.                 }
  28.                 return;
  29.             }
  30.             if ($exception instanceof UserWarning) {
  31.                 if ($exception->getPrevious()) {
  32.                     $this->log($exception->getPrevious(), LogLevel::WARNING);
  33.                 }
  34.                 return;
  35.             }
  36.             $this->log($error->getPrevious(), LogLevel::CRITICAL);
  37.         }
  38.     }
  39.     /**
  40.      * @param \Throwable $throwable
  41.      * @param string     $errorLevel
  42.      */
  43.     public function log($throwable$errorLevel LogLevel::ERROR)
  44.     {
  45.         $this->logger->$errorLevel(self::serializeThrowableObject($throwable), ['throwable' => $throwable]);
  46.     }
  47.     /**
  48.      * @param \Throwable $throwable
  49.      *
  50.      * @return string
  51.      */
  52.     private static function serializeThrowableObject($throwable)
  53.     {
  54.         $message = \sprintf(
  55.             '[GraphQL] %s: %s[%d] (caught throwable) at %s line %s.',
  56.             \get_class($throwable),
  57.             $throwable->getMessage(),
  58.             $throwable->getCode(),
  59.             $throwable->getFile(),
  60.             $throwable->getLine()
  61.         );
  62.         return $message;
  63.     }
  64. }