From 7f97445b51f32446cb8befe01db66df6aac635c7 Mon Sep 17 00:00:00 2001 From: Thibaud Fabre Date: Tue, 22 Sep 2015 12:53:56 +0200 Subject: [PATCH] Log error in exception handler --- lib/Alchemy/Phrasea/Application.php | 1 + .../Phrasea/Core/PhraseaExceptionHandler.php | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/Alchemy/Phrasea/Application.php b/lib/Alchemy/Phrasea/Application.php index 03e86d478c..03feb40a29 100644 --- a/lib/Alchemy/Phrasea/Application.php +++ b/lib/Alchemy/Phrasea/Application.php @@ -267,6 +267,7 @@ class Application extends SilexApplication $this['phraseanet.exception_handler'] = $this->share(function ($app) { $handler = PhraseaExceptionHandler::register($app['debug']); $handler->setTranslator($app['translator']); + $handler->setLogger($app['monolog']); return $handler; }); diff --git a/lib/Alchemy/Phrasea/Core/PhraseaExceptionHandler.php b/lib/Alchemy/Phrasea/Core/PhraseaExceptionHandler.php index ce7d392c90..d81cc3989a 100644 --- a/lib/Alchemy/Phrasea/Core/PhraseaExceptionHandler.php +++ b/lib/Alchemy/Phrasea/Core/PhraseaExceptionHandler.php @@ -11,6 +11,8 @@ namespace Alchemy\Phrasea\Core; +use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler; use Symfony\Component\Debug\Exception\FlattenException; use Symfony\Component\HttpFoundation\Request; @@ -21,6 +23,18 @@ class PhraseaExceptionHandler extends SymfonyExceptionHandler { private $translator; + private $logger; + + public function __construct() + { + $this->logger = new NullLogger(); + } + + public function setLogger(LoggerInterface $logger) + { + $this->logger = $logger; + } + public function setTranslator(TranslatorInterface $translator) { $this->translator = $translator; @@ -33,6 +47,13 @@ class PhraseaExceptionHandler extends SymfonyExceptionHandler public function getContent(FlattenException $exception) { + if ($exception->getStatusCode() == '500') { + $this->logger->error($exception->getMessage(), [ + 'code' => $exception->getCode(), + 'trace' => $exception->getTrace() + ]); + } + switch (true) { case 404 === $exception->getStatusCode(): if (null !== $this->translator) {