Skip to content

Commit

Permalink
Errors are written to the log file
Browse files Browse the repository at this point in the history
  • Loading branch information
Finesse committed Jul 30, 2017
1 parent 070a088 commit c7fe0b0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
"license": "MIT",
"authors": [
{
"name": "Finesse",
"name": "Surgie Finesse",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.0",
"slim/slim": "^3.1",
"slim/php-view": "^2.0",
"psr/log": "^1.0",
"monolog/monolog": "^1.17"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions config/dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@

$container = $app->getContainer();

// error handlers
$container['phpErrorHandler'] = function (ContainerInterface $c) {
return new \Src\Handlers\PhpError($c->get('settings')['displayErrorDetails'], $c->get('logger'));
};
$container['errorHandler'] = function (ContainerInterface $c) {
return $c->get('phpErrorHandler');
};

// view renderer
$container['renderer'] = function (ContainerInterface $c) {
$settings = $c->get('settings')['renderer'];
Expand Down
44 changes: 44 additions & 0 deletions src/Handlers/PhpError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Src\Handlers;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Slim\Handlers\PhpError as BasePhpError;

/**
* Class Error
*
* Custom error handler that writes error messages to logger.
*
* @author Finesse
* @package Src\Handlers
*/
class PhpError extends BasePhpError
{
/**
* @var LoggerInterface Logger for writing error messages
*/
protected $logger;

/**
* {@inheritDoc}
* @param LoggerInterface $logger A logger for writing error messages
*/
public function __construct(bool $displayErrorDetails = false, LoggerInterface $logger)
{
parent::__construct($displayErrorDetails);
$this->logger = $logger;
}

/**
* {@inheritdoc}
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, \Throwable $error)
{
// Log the message
$this->logger->error($error);
return parent::__invoke($request, $response, $error);
}
}

0 comments on commit c7fe0b0

Please sign in to comment.