Chris@0: logger = $logger; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Log 403 errors. Chris@0: * Chris@0: * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event Chris@0: * The event to process. Chris@0: */ Chris@0: public function on403(GetResponseForExceptionEvent $event) { Chris@0: $request = $event->getRequest(); Chris@0: $this->logger->get('access denied')->warning('@uri', ['@uri' => $request->getRequestUri()]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Log 404 errors. Chris@0: * Chris@0: * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event Chris@0: * The event to process. Chris@0: */ Chris@0: public function on404(GetResponseForExceptionEvent $event) { Chris@0: $request = $event->getRequest(); Chris@0: $this->logger->get('page not found')->warning('@uri', ['@uri' => $request->getRequestUri()]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Log not-otherwise-specified errors, including HTTP 500. Chris@0: * Chris@0: * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event Chris@0: * The event to process. Chris@0: */ Chris@0: public function onError(GetResponseForExceptionEvent $event) { Chris@0: $exception = $event->getException(); Chris@0: $error = Error::decodeException($exception); Chris@0: $this->logger->get('php')->log($error['severity_level'], '%type: @message in %function (line %line of %file).', $error); Chris@0: Chris@0: $is_critical = !$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500; Chris@0: if ($is_critical) { Chris@0: error_log(sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine())); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Log all exceptions. Chris@0: * Chris@0: * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event Chris@0: * The event to process. Chris@0: */ Chris@0: public function onException(GetResponseForExceptionEvent $event) { Chris@0: $exception = $event->getException(); Chris@0: Chris@0: $method = 'onError'; Chris@0: Chris@0: // Treat any non-HTTP exception as if it were one, so we log it the same. Chris@0: if ($exception instanceof HttpExceptionInterface) { Chris@0: $possible_method = 'on' . $exception->getStatusCode(); Chris@0: if (method_exists($this, $possible_method)) { Chris@0: $method = $possible_method; Chris@0: } Chris@0: } Chris@0: Chris@0: $this->$method($event); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function getSubscribedEvents() { Chris@0: $events[KernelEvents::EXCEPTION][] = ['onException', 50]; Chris@0: return $events; Chris@0: } Chris@0: Chris@0: }