This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nik Barham
committed
Dec 13, 2017
1 parent
da651fb
commit 6944f7d
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |