Skip to content

Commit

Permalink
Adding logging and error handlaing for production environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipe Silva committed Feb 2, 2015
1 parent 4282e72 commit 17a1d08
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 7 deletions.
15 changes: 11 additions & 4 deletions src/Slick/Log/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
namespace Slick\Log;

use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Slick\Common\Base;
use Slick\Configuration\Configuration;
use Slick\Log\Handler\NullHandler;
use Slick\Configuration\Exception\FileNotFoundException;

/**
* Factory for a Monolog logger.
Expand Down Expand Up @@ -52,14 +54,14 @@ class Log extends Base
* @readwrite
* @var string
*/
protected $_prefix = '';
protected $_prefix;

/**
* Gets the logger for the channel with the provided name.
*
* @param string $name The loggers channel name to retrieve.
*
* @return \Monolog\Logger The logger object for the given channel name.
* @return \Monolog\Logger|LoggerInterface The logger object for the given channel name.
*/
public static function logger($name = null)
{
Expand Down Expand Up @@ -111,8 +113,13 @@ public function getPrefix()
{
if (is_null($this->_prefix)) {
$hostName = gethostname();
$this->_prefix = Configuration::get('config')
->get('logger.prefix', $hostName);
try {
$this->_prefix = Configuration::get('config')
->get('logger.prefix', $hostName);
} catch(FileNotFoundException $exp) {
$this->_prefix = $hostName;
}

}
return $this->_prefix;
}
Expand Down
14 changes: 11 additions & 3 deletions src/Slick/Mvc/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Slick\Di\Definition;
use Psr\Log\LoggerInterface;
use Slick\Log\Log;
use Slick\Mvc\Whoops\Handler\Production;
use Slick\Template\Template;
use Slick\Di\ContainerBuilder;
use Slick\Mvc\Events\Dispatch;
Expand Down Expand Up @@ -325,11 +326,18 @@ protected function _startErrorHandler()
$environment = $this->getConfiguration()
->get('environment', 'production');

if ($environment != 'production') {
$handler = new PrettyPageHandler();
$this->_whoops->pushHandler($handler);
switch ($environment) {
case 'production':
$handler = new Production();
$handler->setApplication($this);
break;

default:
$handler = new PrettyPageHandler();
}

$this->_whoops->pushHandler($handler);

$this->_whoops->register();
return $this;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Slick/Mvc/Views/errors/404.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<title>{{ translate('File not found') }}</title>
</head>
<body>
<h1>{{ translate('File not found') }}</h1>
<p>{{ translate('The file you specify is not found in this server.') }}</a></p>
</body>
</html>
11 changes: 11 additions & 0 deletions src/Slick/Mvc/Views/errors/500.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<title>{{ translate ('Sorry...') }}</title>
</head>
<body>
<h1>{{ translate ('Sorry...') }}</h1>
<h3>{{ translate ("It\'s not you. It\'s us.") }}</h3>
<p>{{ translate ("We\'re experiencing an internal server problem. <br/>
Please try again later or contact <a href=\"#\">a system adminitrator.</a>")|raw }}</p>
</body>
</html>
87 changes: 87 additions & 0 deletions src/Slick/Mvc/Whoops/Handler/Production.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/**
* Production error handler
*
* @package Slick\Mvc\Whoops\Handler
* @author Filipe Silva <silvam.filipe@gmail.com>
* @copyright 2014 Filipe Silva
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @since Version 1.1.0
*/

namespace Slick\Mvc\Whoops\Handler;

use Slick\Log\Log;
use Slick\Mvc\Application;
use Slick\Template\Template;
use Whoops\Handler\PlainTextHandler;
use Slick\Mvc\Exception\ActionNotFoundException;
use Slick\Mvc\Exception\ControllerNotFoundException;

/**
* Production error handler
*
* @package Slick\Mvc\Whoops\Handler
* @author Filipe Silva <silvam.filipe@gmail.com>
*/
class Production extends PlainTextHandler
{

/**
* @var Application
*/
private $app;

/**
* Sets application
*
* @param Application $app
* @return self
*/
public function setApplication(Application $app)
{
$this->app = $app;
return $this;
}

/**
* Handle the exception
*/
public function handle()
{
$exception = $this->getException();

Log::logger('ErrorHandler')->error($exception->getMessage(), [
'exception' => get_class($exception),
'file' => sprintf(
"%s on line %d",
$exception->getFile(),
$exception->getLine()
)
]);

$template = new Template();

$template = $template->initialize();
$view = 'errors/500.html.twig';
$code = 500;
if (
($exception instanceof ControllerNotFoundException) ||
($exception instanceof ActionNotFoundException)
) {
$view = 'errors/404.html.twig';
$code = 404;
}

$data = ['exception' => $exception];
$html = $template->parse($view)
->process($data);

$this->app->response->setStatusCode($code)
->setContent($html);

$this->app->response->send();
}

}

0 comments on commit 17a1d08

Please sign in to comment.