mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-23 18:03:17 +00:00
Merge pull request #1516 from aztech-dev/logging-api
Enable application logging from API calls
This commit is contained in:
@@ -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());
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user