Skip to content

Commit

Permalink
Merge pull request #400 from sergeyklay/feature_error_log_formatter
Browse files Browse the repository at this point in the history
Added support for formatter to Phalcon\Error\Handler
  • Loading branch information
sergeyklay committed Aug 16, 2015
2 parents ab5955f + 2046543 commit 62aa54f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
6 changes: 2 additions & 4 deletions Library/Phalcon/Error/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com) |
| Copyright (c) 2011-2015 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
Expand Down Expand Up @@ -66,9 +66,7 @@ public function main()
private function registerAutoloaders()
{
$loader = new Loader();
$loader->registerNamespaces(array(
'Phalcon\Error' => '.',
));
$loader->registerNamespaces(['Phalcon\Error' => '.']);
$loader->register();
}

Expand Down
9 changes: 4 additions & 5 deletions Library/Phalcon/Error/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com) |
| Copyright (c) 2011-2015 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
Expand All @@ -21,7 +21,6 @@

class Error
{

/**
* @var array
*/
Expand All @@ -32,17 +31,17 @@ class Error
*
* @param array $options
*/
public function __construct(array $options = array())
public function __construct(array $options = [])
{
$defaults = array(
$defaults = [
'type' => -1,
'message' => 'No error message',
'file' => '',
'line' => '',
'exception' => null,
'isException' => false,
'isError' => false,
);
];

$options = array_merge($defaults, $options);

Expand Down
33 changes: 19 additions & 14 deletions Library/Phalcon/Error/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com) |
| Copyright (c) 2011-2015 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
Expand All @@ -15,15 +15,16 @@
| Authors: Andres Gutierrez <[email protected]> |
| Eduar Carvajal <[email protected]> |
| Nikita Vershinin <[email protected]> |
| Serghei Iakovlev <[email protected]> |
+------------------------------------------------------------------------+
*/
namespace Phalcon\Error;

use Phalcon\DI;
use Phalcon\Di;
use Phalcon\Logger\Formatter;

class Handler
{

/**
* Registers itself as error and exception handler.
*
Expand All @@ -50,26 +51,26 @@ public static function register()
return;
}

$options = array(
$options = [
'type' => $errno,
'message' => $errstr,
'file' => $errfile,
'line' => $errline,
'isError' => true,
);
];

static::handle(new Error($options));
});

set_exception_handler(function (\Exception $e) {
$options = array(
$options = [
'type' => $e->getCode(),
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'isException' => true,
'exception' => $e,
);
];

static::handle(new Error($options));
});
Expand All @@ -89,12 +90,16 @@ public static function register()
*/
public static function handle(Error $error)
{
$di = DI::getDefault();
$config = $di->getShared('config');
$di = Di::getDefault();
$config = $di->getShared('config')->error;
$type = static::getErrorType($error->type());
$message = "$type: {$error->message()} in {$error->file()} on line {$error->line()}";

$config->error->logger->log($message);
if (isset($config->formatter) && $config->formatter instanceof Formatter) {
$config->logger->setFormatter($config->formatter);
}

$config->logger->log($message);

switch ($error->type()) {
case E_WARNING:
Expand All @@ -119,13 +124,13 @@ public static function handle(Error $error)
$view = $di->getShared('view');
$response = $di->getShared('response');

$dispatcher->setControllerName($config->error->controller);
$dispatcher->setActionName($config->error->action);
$dispatcher->setParams(array('error' => $error));
$dispatcher->setControllerName($config->controller);
$dispatcher->setActionName($config->action);
$dispatcher->setParams(['error' => $error]);

$view->start();
$dispatcher->dispatch();
$view->render($config->error->controller, $config->error->action, $dispatcher->getParams());
$view->render($config->controller, $config->action, $dispatcher->getParams());
$view->finish();

return $response->setContent($view->getContent())->send();
Expand Down
9 changes: 4 additions & 5 deletions Library/Phalcon/Error/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ Configuration
-------------

For the error handler to work properly, following section has to be created
in the configuration file (in this case php array). All options are mandatory:
in the configuration file (in this case php array). The `logger`, `controller`, `action` options are mandatory:

```php

<?php
return [
'error' => [
'logger' => new \Phalcon\Logger\Adapter\File(ROOT_PATH . '/log/' . APPLICATION_ENV . '.log'),
'formatter' => new \Phalcon\Logger\Formatter\Line('[%date%][%type%] %message%', 'Y-m-d H:i:s O'),
'controller' => 'error',
'action' => 'index',
]
Expand All @@ -25,6 +25,7 @@ return [

* `logger` defines an object used for logging. It has to implement `log` method in order for
error handler to work properly.
* `formatter` sets the message formatter.
* `controller` is the name of error controller, which will be dispatched, when an exception or error
occurs.
* `action` is the name of action in the error controller, which will be called, when an exception or error
Expand All @@ -34,7 +35,6 @@ In the Application file (please take a look at \Phalcon\Error\Application for re
error handler has to be registered. Application must also define constants for application environments:

```php

<?php
class Application extends \Phalcon\Mvc\Application
{
Expand All @@ -55,7 +55,6 @@ class Application extends \Phalcon\Mvc\Application
In the error controller \Phalcon\Error\Error can be retrieved through the dispatcher:

```php

public function indexAction()
{
$error = $this->dispatcher->getParam('error');
Expand Down Expand Up @@ -84,4 +83,4 @@ Error message could be displayed to the user this way:
<pre><?php echo $error->exception()->getTraceAsString(); ?></pre>
<?php } ?>
<?php endif; ?>
```
```

0 comments on commit 62aa54f

Please sign in to comment.