1
0
mirror of https://github.com/flarum/core.git synced 2025-08-04 23:47:32 +02:00

Merge pull request #1633 from flarum/tz/improve-logging

Improve logging
This commit is contained in:
Franz Liedke
2018-11-13 11:03:24 +01:00
committed by GitHub
6 changed files with 111 additions and 13 deletions

View File

@@ -25,7 +25,6 @@ use Flarum\Http\RouteCollection;
use Flarum\Http\RouteHandlerFactory; use Flarum\Http\RouteHandlerFactory;
use Flarum\Http\UrlGenerator; use Flarum\Http\UrlGenerator;
use Tobscure\JsonApi\ErrorHandler; use Tobscure\JsonApi\ErrorHandler;
use Tobscure\JsonApi\Exception\Handler\FallbackExceptionHandler;
use Tobscure\JsonApi\Exception\Handler\InvalidParameterExceptionHandler; use Tobscure\JsonApi\Exception\Handler\InvalidParameterExceptionHandler;
use Zend\Stratigility\MiddlewarePipe; use Zend\Stratigility\MiddlewarePipe;
@@ -80,7 +79,7 @@ class ApiServiceProvider extends AbstractServiceProvider
$handler->registerHandler(new ExceptionHandler\TokenMismatchExceptionHandler); $handler->registerHandler(new ExceptionHandler\TokenMismatchExceptionHandler);
$handler->registerHandler(new ExceptionHandler\ValidationExceptionHandler); $handler->registerHandler(new ExceptionHandler\ValidationExceptionHandler);
$handler->registerHandler(new InvalidParameterExceptionHandler); $handler->registerHandler(new InvalidParameterExceptionHandler);
$handler->registerHandler(new FallbackExceptionHandler($this->app->inDebugMode())); $handler->registerHandler(new ExceptionHandler\FallbackExceptionHandler($this->app->inDebugMode(), $this->app->make('log')));
return $handler; return $handler;
}); });

View File

@@ -12,6 +12,7 @@
namespace Flarum\Api; namespace Flarum\Api;
use Exception; use Exception;
use Throwable;
use Tobscure\JsonApi\Document; use Tobscure\JsonApi\Document;
use Tobscure\JsonApi\ErrorHandler as JsonApiErrorHandler; use Tobscure\JsonApi\ErrorHandler as JsonApiErrorHandler;
@@ -34,8 +35,12 @@ class ErrorHandler
* @param Exception $e * @param Exception $e
* @return JsonApiResponse * @return JsonApiResponse
*/ */
public function handle(Exception $e) public function handle(Throwable $e)
{ {
if (! $e instanceof Exception) {
$e = new Exception($e->getMessage(), $e->getCode(), $e);
}
$response = $this->errorHandler->handle($e); $response = $this->errorHandler->handle($e);
$document = new Document; $document = new Document;

View File

@@ -0,0 +1,77 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\ExceptionHandler;
use Exception;
use Psr\Log\LoggerInterface;
use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface;
use Tobscure\JsonApi\Exception\Handler\ResponseBag;
class FallbackExceptionHandler implements ExceptionHandlerInterface
{
/**
* @var bool
*/
protected $debug;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @param bool $debug
* @param LoggerInterface $logger
*/
public function __construct($debug, LoggerInterface $logger)
{
$this->debug = $debug;
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public function manages(Exception $e)
{
return true;
}
/**
* {@inheritdoc}
*/
public function handle(Exception $e)
{
$status = 500;
$error = $this->constructError($e, $status);
$this->logger->error($e);
return new ResponseBag($status, [$error]);
}
/**
* @param Exception $e
* @param $status
* @return array
*/
private function constructError(Exception $e, $status)
{
$error = ['code' => $status, 'title' => 'Internal server error'];
if ($this->debug) {
$error['detail'] = (string) $e;
}
return $error;
}
}

View File

@@ -11,12 +11,12 @@
namespace Flarum\Api\Middleware; namespace Flarum\Api\Middleware;
use Exception;
use Flarum\Api\ErrorHandler; use Flarum\Api\ErrorHandler;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface as Middleware; use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as Handler; use Psr\Http\Server\RequestHandlerInterface as Handler;
use Throwable;
class HandleErrors implements Middleware class HandleErrors implements Middleware
{ {
@@ -40,7 +40,7 @@ class HandleErrors implements Middleware
{ {
try { try {
return $handler->handle($request); return $handler->handle($request);
} catch (Exception $e) { } catch (Throwable $e) {
return $this->errorHandler->handle($e); return $this->errorHandler->handle($e);
} }
} }

View File

@@ -11,7 +11,6 @@
namespace Flarum\Http\Middleware; namespace Flarum\Http\Middleware;
use Exception;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\View\Factory as ViewFactory; use Illuminate\Contracts\View\Factory as ViewFactory;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
@@ -20,6 +19,7 @@ use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as Handler; use Psr\Http\Server\RequestHandlerInterface as Handler;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\Translation\TranslatorInterface; use Symfony\Component\Translation\TranslatorInterface;
use Throwable;
use Zend\Diactoros\Response\HtmlResponse; use Zend\Diactoros\Response\HtmlResponse;
class HandleErrorsWithView implements Middleware class HandleErrorsWithView implements Middleware
@@ -65,12 +65,12 @@ class HandleErrorsWithView implements Middleware
{ {
try { try {
return $handler->handle($request); return $handler->handle($request);
} catch (Exception $e) { } catch (Throwable $e) {
return $this->formatException($e); return $this->formatException($e);
} }
} }
protected function formatException(Exception $error) protected function formatException(Throwable $error)
{ {
$status = 500; $status = 500;
$errorCode = $error->getCode(); $errorCode = $error->getCode();
@@ -81,11 +81,10 @@ class HandleErrorsWithView implements Middleware
$status = $errorCode; $status = $errorCode;
} }
// Log the exception (with trace)
$this->logger->debug($error);
if (! $this->view->exists($name = "flarum.forum::error.$status")) { if (! $this->view->exists($name = "flarum.forum::error.$status")) {
$name = 'flarum.forum::error.default'; $name = 'flarum.forum::error.default';
$this->logger->error($error);
} }
$view = $this->view->make($name) $view = $this->view->make($name)

View File

@@ -11,15 +11,29 @@
namespace Flarum\Http\Middleware; namespace Flarum\Http\Middleware;
use Exception;
use Franzl\Middleware\Whoops\WhoopsRunner; use Franzl\Middleware\Whoops\WhoopsRunner;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface as Middleware; use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as Handler; use Psr\Http\Server\RequestHandlerInterface as Handler;
use Psr\Log\LoggerInterface;
use Throwable;
class HandleErrorsWithWhoops implements Middleware class HandleErrorsWithWhoops implements Middleware
{ {
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/** /**
* Catch all errors that happen during further middleware execution. * Catch all errors that happen during further middleware execution.
*/ */
@@ -27,7 +41,11 @@ class HandleErrorsWithWhoops implements Middleware
{ {
try { try {
return $handler->handle($request); return $handler->handle($request);
} catch (Exception $e) { } catch (Throwable $e) {
if ($e->getCode() !== 404) {
$this->logger->error($e);
}
return WhoopsRunner::handle($e, $request); return WhoopsRunner::handle($e, $request);
} }
} }