Added simple error/exception handler and logging
This commit is contained in:
parent
3aa4806e25
commit
92647c6bf9
100
PHPCI/Logging/Handler.php
Normal file
100
PHPCI/Logging/Handler.php
Normal file
|
@ -0,0 +1,100 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCI\Logging;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Handler
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $levels = array(
|
||||
E_WARNING => 'Warning',
|
||||
E_NOTICE => 'Notice',
|
||||
E_USER_ERROR => 'User Error',
|
||||
E_USER_WARNING => 'User Warning',
|
||||
E_USER_NOTICE => 'User Notice',
|
||||
E_STRICT => 'Runtime Notice',
|
||||
E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
|
||||
E_DEPRECATED => 'Deprecated',
|
||||
E_USER_DEPRECATED => 'User Deprecated',
|
||||
);
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
public function __construct(LoggerInterface $logger = NULL)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public static function register(LoggerInterface $logger = NULL)
|
||||
{
|
||||
$handler = new static($logger);
|
||||
|
||||
set_error_handler(array($handler, 'handleError'));
|
||||
register_shutdown_function(array($handler, 'handleFatalError'));
|
||||
|
||||
set_exception_handler(array($handler, 'handleException'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param integer $level
|
||||
* @param string $message
|
||||
* @param string $file
|
||||
* @param integer $line
|
||||
*
|
||||
* @throws \ErrorException
|
||||
*/
|
||||
public function handleError($level, $message, $file, $line)
|
||||
{
|
||||
if (error_reporting() & $level) {
|
||||
|
||||
$exception_level = isset($this->levels[$level]) ? $this->levels[$level] : $level;
|
||||
|
||||
throw new \ErrorException(
|
||||
sprintf('%s: %s in %s line %d', $exception_level, $message, $file, $line),
|
||||
0, $level, $file, $line
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \ErrorException
|
||||
*/
|
||||
public function handleFatalError()
|
||||
{
|
||||
$fatal_error = error_get_last();
|
||||
|
||||
if ($fatal_error['type'] === E_ERROR) {
|
||||
|
||||
$e = new \ErrorException(
|
||||
sprintf('%s: %s in %s line %d', $fatal_error['type'], $fatal_error['message'], $fatal_error['file'], $fatal_error['line']),
|
||||
0, $fatal_error['type'], $fatal_error['file'], $fatal_error['line']
|
||||
);
|
||||
$this->log($e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Exception $exception
|
||||
*/
|
||||
public function handleException(\Exception $exception)
|
||||
{
|
||||
$this->log($exception);
|
||||
}
|
||||
|
||||
protected function log(\Exception $exception)
|
||||
{
|
||||
if (null !== $this->logger) {
|
||||
|
||||
$message = sprintf(
|
||||
'%s: %s (uncaught exception) at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()
|
||||
);
|
||||
$this->logger->error($message);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -22,7 +22,7 @@ class LoggerConfig {
|
|||
public static function newFromFile($filePath)
|
||||
{
|
||||
if (file_exists($filePath)) {
|
||||
$configArray = require($filePath);
|
||||
$configArray = require_once($filePath);
|
||||
}
|
||||
else {
|
||||
$configArray = array();
|
||||
|
@ -48,7 +48,7 @@ class LoggerConfig {
|
|||
*/
|
||||
public function getFor($name) {
|
||||
$handlers = $this->getHandlers(self::KEY_AlwaysLoaded);
|
||||
$handlers = array_merge($handlers, $this->getHandlers($name));
|
||||
//$handlers = array_merge($handlers, $this->getHandlers($name));
|
||||
return new Logger($name, $handlers);
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,9 @@
|
|||
*/
|
||||
|
||||
// Let PHP take a guess as to the default timezone, if the user hasn't set one:
|
||||
use PHPCI\Logging\Handler;
|
||||
use PHPCI\Logging\LoggerConfig;
|
||||
|
||||
date_default_timezone_set(@date_default_timezone_get());
|
||||
|
||||
// Set up a basic autoloader for PHPCI:
|
||||
|
@ -37,10 +40,12 @@ if (!file_exists(dirname(__FILE__) . '/vendor/autoload.php') && defined('PHPCI_I
|
|||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
// Load Composer autoloader:
|
||||
require_once(dirname(__FILE__) . '/vendor/autoload.php');
|
||||
|
||||
$loggerConfig = LoggerConfig::newFromFile(__DIR__ . "/loggerconfig.php");
|
||||
Handler::register($loggerConfig->getFor('Exceptions'));
|
||||
|
||||
// Load configuration if present:
|
||||
$conf = array();
|
||||
$conf['b8']['app']['namespace'] = 'PHPCI';
|
||||
|
|
4
console
4
console
|
@ -21,8 +21,6 @@ use PHPCI\Command\PollCommand;
|
|||
use PHPCI\Command\CreateAdminCommand;
|
||||
use Symfony\Component\Console\Application;
|
||||
|
||||
$loggerConfig = \PHPCI\Logging\LoggerConfig::newFromFile(__DIR__ . "/loggerconfig.php");
|
||||
|
||||
$application = new Application();
|
||||
|
||||
$application->add(new RunCommand($loggerConfig->getFor('RunCommand')));
|
||||
|
@ -33,4 +31,4 @@ $application->add(new DaemonCommand($loggerConfig->getFor('DaemonCommand')));
|
|||
$application->add(new PollCommand($loggerConfig->getFor('PollCommand')));
|
||||
$application->add(new CreateAdminCommand);
|
||||
|
||||
$application->run();
|
||||
$application->run();
|
Loading…
Reference in a new issue