Skip to content

Commit

Permalink
Merge pull request #6 from victorwelling/logging-middleware
Browse files Browse the repository at this point in the history
Added error logging to logging middleware
  • Loading branch information
Victor Welling committed Aug 31, 2018
2 parents 2ebeea9 + f7088ef commit d9bb30f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 20 deletions.
52 changes: 32 additions & 20 deletions src/Middleware/LoggingMiddleware.php
Expand Up @@ -8,6 +8,7 @@
use Shoot\Shoot\HasPresenterInterface;
use Shoot\Shoot\MiddlewareInterface;
use Shoot\Shoot\View;
use Throwable;

/**
* Logs all views being processed by Shoot. It's recommended to add this before any other middleware.
Expand Down Expand Up @@ -36,36 +37,47 @@ public function __construct(LoggerInterface $logger)
* @param callable $next
*
* @return View
*
* @throws Throwable
*/
public function process(View $view, ServerRequestInterface $request, callable $next): View
{
$startTime = microtime(true);
$context = [];
$message = $view->getName();

/** @var View $view */
$view = $next($view);
try {
$startTime = microtime(true);

$endTime = microtime(true);
/** @var View $view */
$view = $next($view);

$presentationModel = $view->getPresentationModel();
$endTime = microtime(true);

$fields = [
'presentation_model' => $presentationModel->getName(),
'time_taken' => sprintf("%f seconds", $endTime - $startTime),
'variables' => $presentationModel->getVariables(),
];
$presentationModel = $view->getPresentationModel();

if ($presentationModel instanceof HasPresenterInterface) {
$fields['presenter_name'] = $presentationModel->getPresenterName();
}
$context['presentation_model'] = $presentationModel->getName();

if ($view->hasSuppressedException()) {
$fields['exception'] = $view->getSuppressedException();
if ($presentationModel instanceof HasPresenterInterface) {
$context['presenter_name'] = $presentationModel->getPresenterName();
}

$this->logger->warning($view->getName(), $fields);
} else {
$this->logger->debug($view->getName(), $fields);
}
$context['time_taken'] = sprintf("%f seconds", $endTime - $startTime);

if ($view->hasSuppressedException()) {
$context['exception'] = $view->getSuppressedException();

return $view;
$this->logger->warning($message, $context);
} else {
$this->logger->debug($message, $context);
}

return $view;
} catch (Throwable $exception) {
$context['exception'] = $exception;

$this->logger->error($message, $context);

throw $exception;
}
}
}
28 changes: 28 additions & 0 deletions tests/Middleware/LoggingMiddlewareTest.php
Expand Up @@ -3,6 +3,7 @@

namespace Shoot\Shoot\Tests\Middleware;

use Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -79,4 +80,31 @@ public function testShouldLogSuppressedExceptions()
$middleware = new LoggingMiddleware($logger);
$middleware->process($view, $this->request, $this->next);
}

/**
* @return void
*/
public function testShouldLogUncaughtExceptions()
{
$view = ViewFactory::create();

$next = function () {
throw new Exception();
};

/** @var LoggerInterface|MockObject $logger */
$logger = $this->createMock(LoggerInterface::class);
$logger
->expects($this->once())
->method('error')
->with(
$this->equalTo('item.twig'),
$this->arrayHasKey('exception')
);

$this->expectException(Exception::class);

$middleware = new LoggingMiddleware($logger);
$middleware->process($view, $this->request, $next);
}
}

0 comments on commit d9bb30f

Please sign in to comment.