From 92647c6bf9aa0b678e6da4108bd29fa5c5acf34f Mon Sep 17 00:00:00 2001 From: Corpsee Date: Tue, 18 Mar 2014 01:10:47 +0700 Subject: [PATCH] Added simple error/exception handler and logging --- PHPCI/Logging/Handler.php | 100 +++++++++++++++++++++++++++++++++ PHPCI/Logging/LoggerConfig.php | 4 +- bootstrap.php | 7 ++- console | 4 +- 4 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 PHPCI/Logging/Handler.php diff --git a/PHPCI/Logging/Handler.php b/PHPCI/Logging/Handler.php new file mode 100644 index 00000000..3ae5e93f --- /dev/null +++ b/PHPCI/Logging/Handler.php @@ -0,0 +1,100 @@ + '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); + } + } +} \ No newline at end of file diff --git a/PHPCI/Logging/LoggerConfig.php b/PHPCI/Logging/LoggerConfig.php index 5c579f6e..3eb0a5b2 100644 --- a/PHPCI/Logging/LoggerConfig.php +++ b/PHPCI/Logging/LoggerConfig.php @@ -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); } diff --git a/bootstrap.php b/bootstrap.php index 01301dbb..a8ececf3 100755 --- a/bootstrap.php +++ b/bootstrap.php @@ -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'; diff --git a/console b/console index 8a65f1c5..43fc2b8f 100755 --- a/console +++ b/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(); \ No newline at end of file