Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Commit

Permalink
New ErrorLogger middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Nik Barham committed Dec 13, 2017
1 parent da651fb commit 6944f7d
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Middleware/ErrorLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Circuit\Middleware;

use Circuit\Interfaces\Middleware;
use Circuit\Interfaces\Delegate;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Error Logger
*
* A middleware that pushes exceptions out into a logger
*
* @author Nik Barham <[email protected]>
*/
class ErrorLogger implements Middleware
{
/**
* Constructor
*
* @param LoggerInterface $log Logger to log error to
*/
public function __construct(LoggerInterface $log)
{
$this->log = $log;
}

/**
* Run Middleware for a particular request
*
* @param Request $request HTTP Foundation Request object
* @param Delegate $delegate Either the Router or HandlerContainer, depending on whether this is run pre or post
* routing
* @return Response
*/
public function process(Request $request, Delegate $delegate) : Response
{
try {
return $delegate->process($request);
} catch (\Throwable $e) {
$this->log->error($e->getMessage(), ['trace' => $e->getTrace(), 'line' => $e->getLine(), 'file' => $e->getFile(), 'code' => $e->getCode()]);
throw $e;
}
}
}

0 comments on commit 6944f7d

Please sign in to comment.