66use MattJanssen \ApiResponseBundle \Compiler \ApiConfigCompiler ;
77use MattJanssen \ApiResponseBundle \Exception \ApiResponseExceptionInterface ;
88use MattJanssen \ApiResponseBundle \Generator \ApiResponseGenerator ;
9+ use Psr \Log \LoggerInterface ;
910use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
1011use Symfony \Component \HttpFoundation \Request ;
1112use Symfony \Component \HttpFoundation \Response ;
@@ -44,6 +45,13 @@ class ApiResponseSubscriber implements EventSubscriberInterface
4445 */
4546 private $ configCompiler ;
4647
48+ /**
49+ * PSR Logger
50+ *
51+ * @var LoggerInterface
52+ */
53+ private $ logger ;
54+
4755 /**
4856 * Kernel's Debug Status
4957 *
@@ -56,15 +64,18 @@ class ApiResponseSubscriber implements EventSubscriberInterface
5664 *
5765 * @param ApiResponseGenerator $responseGenerator
5866 * @param ApiConfigCompiler $configCompiler
67+ * @param LoggerInterface $logger
5968 * @param bool $debug
6069 */
6170 public function __construct (
6271 ApiResponseGenerator $ responseGenerator ,
6372 ApiConfigCompiler $ configCompiler ,
73+ LoggerInterface $ logger ,
6474 $ debug
6575 ) {
6676 $ this ->responseGenerator = $ responseGenerator ;
6777 $ this ->configCompiler = $ configCompiler ;
78+ $ this ->logger = $ logger ;
6879 $ this ->debug = $ debug ;
6980 }
7081
@@ -210,6 +221,14 @@ public function onKernelException(GetResponseForExceptionEvent $event)
210221 );
211222
212223 $ event ->setResponse ($ response );
224+
225+ if ($ httpCode >= Response::HTTP_INTERNAL_SERVER_ERROR ) {
226+ // Log exceptions that result in a 5xx server response.
227+ $ this ->logger ->critical (
228+ sprintf ('API Exception %s: "%s" at %s line %s ' , get_class ($ exception ), $ exception ->getMessage (), $ exception ->getFile (), $ exception ->getLine ()),
229+ ['exception ' => $ exception ]
230+ );
231+ }
213232 }
214233
215234 /**
0 commit comments