Skip to content

Commit

Permalink
handle exception and error to db implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Nov 10, 2016
1 parent d5b0b8f commit d87d253
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
1 change: 1 addition & 0 deletions data/db.mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ CREATE TABLE `log` (
`url` varchar(2000) NOT NULL,
`file` varchar(2000) NOT NULL,
`line` int(11) NOT NULL,
`error_type` varchar(255) NOT NULL,
PRIMARY KEY (`date`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
6 changes: 5 additions & 1 deletion spec/Listener/MvcSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@
],
],
];
$this->logging = Double::instance(['extends' => Logging::class, 'methods' => '__construct']);

$this->logging = Double::instance([
'extends' => Logging::class,
'methods' => '__construct'
]);

return new Mvc(
$config,
Expand Down
31 changes: 22 additions & 9 deletions src/Handler/Logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace ErrorHeroModule\Handler;

use ErrorHeroModule\Listener\Mvc;
use Error;
use Exception;
use ErrorException;
use Zend\Log\Logger;
use Zend\Log\Writer\Db;

class Logging
{
Expand All @@ -14,12 +16,19 @@ class Logging
*/
private $logger;

/**
* @var string
*/
private $requestUri;

/**
* @param Logger $logger
* @param string $requestUri
*/
public function __construct(Logger $logger)
public function __construct(Logger $logger, $requestUri)
{
$this->logger = $logger;
$this->logger = $logger;
$this->requestUri = $requestUri;
}

/**
Expand All @@ -43,9 +52,10 @@ public function handleException($e)
$implodeMessages = implode("\r\n", $messages);

$extra = [
'url' => 'url',
'file' => $errorFile,
'line' => $errorLine,
'url' => $this->requestUri,
'file' => $errorFile,
'line' => $errorLine,
'error_type' => get_class($e),
];
$this->logger->log($priority, $implodeMessages, $extra);
}
Expand All @@ -55,19 +65,22 @@ public function handleException($e)
* @param string $errorMessage
* @param string $errorFile
* @param int $errorLine
* @param string $errorTypeString
*/
public function handleError(
$errorType,
$errorMessage,
$errorFile,
$errorLine
$errorLine,
$errorTypeString
) {
$priority = Logger::$errorPriorityMap[$errorType];

$extra = [
'url' => 'url',
'file' => $errorFile,
'line' => $errorLine,
'url' => $this->requestUri,
'file' => $errorFile,
'line' => $errorLine,
'error_type' => $errorTypeString,
];
$this->logger->log($priority, $errorMessage, $extra);
}
Expand Down
10 changes: 9 additions & 1 deletion src/Handler/LoggingFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

namespace ErrorHeroModule\Handler;

use Zend\Console\Console;

class LoggingFactory
{
public function __invoke($container)
{
$requestUri = '';
if (! Console::isConsole()) {
$requestUri = $container->get('Request')->getRequestUri();
}

return new Logging(
$container->get('ErrorHeroModuleLogger')
$container->get('ErrorHeroModuleLogger'),
$requestUri
);
}
}
3 changes: 2 additions & 1 deletion src/Listener/Mvc.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ public function phpErrorHandler($errorType, $errorMessage, $errorFile, $errorLin
$errorType,
$errorMessage,
$errorFile,
$errorLine
$errorLine,
$errorTypeString
);
}
}

0 comments on commit d87d253

Please sign in to comment.