Merge pull request #1516 from aztech-dev/logging-api

Enable application logging from API calls
This commit is contained in:
Thibaud Fabre
2015-09-29 18:57:33 +02:00
2 changed files with 21 additions and 1 deletions

View File

@@ -41,7 +41,10 @@ return call_user_func(function ($environment = PhraseaApplication::ENV_PROD) {
$app->loadPlugins();
$app['exception_handler'] = $app->share(function ($app) {
return new ApiExceptionHandlerSubscriber($app);
$handler = new ApiExceptionHandlerSubscriber($app);
$handler->setLogger($app['monolog']);
return $handler;
});
$app['monolog'] = $app->share($app->extend('monolog', function (Logger $monolog) {
$monolog->pushProcessor(new WebProcessor());

View File

@@ -13,6 +13,8 @@ namespace Alchemy\Phrasea\Core\Event\Subscriber;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Controller\Api\Result;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -27,9 +29,17 @@ class ApiExceptionHandlerSubscriber implements EventSubscriberInterface
{
private $app;
private $logger;
public function __construct(Application $app)
{
$this->app = $app;
$this->logger = new NullLogger();
}
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
public static function getSubscribedEvents()
@@ -64,6 +74,13 @@ class ApiExceptionHandlerSubscriber implements EventSubscriberInterface
$code = 500;
}
if ($code == 500) {
$this->logger->error($e->getMessage(), [
'code' => $e->getCode(),
'trace' => $e->getTrace()
]);
}
if ($e instanceof HttpExceptionInterface) {
$headers = $e->getHeaders();
}