diff --git a/PHPCI/Logging/Handler.php b/PHPCI/Logging/Handler.php new file mode 100644 index 00000000..55e0e4a3 --- /dev/null +++ b/PHPCI/Logging/Handler.php @@ -0,0 +1,109 @@ + '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(); + + try { + if (($e = error_get_last()) !== null) { + $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); + } + } + catch (\Exception $e) + { + $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, array('exception' => $exception)); + } + } +} \ No newline at end of file diff --git a/bootstrap.php b/bootstrap.php index 01301dbb..3cfcf038 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('_')); + // 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 diff --git a/loggerconfig.php.example b/loggerconfig.php.example index dad42f17..c306673b 100644 --- a/loggerconfig.php.example +++ b/loggerconfig.php.example @@ -1,16 +1,16 @@ function() { return array( - new \Monolog\Handler\StreamHandler('c:\temp\errors.log', \Monolog\Logger::ERROR), + new \Monolog\Handler\StreamHandler(__DIR__ . DIRECTORY_SEPARATOR . 'errors.log', \Monolog\Logger::ERROR), ); }, - /** Loggers for the RunCommand */ 'RunCommand' => function() { return array( - new \Monolog\Handler\RotatingFileHandler('c:\temp\everything',3, \Monolog\Logger::INFO), + new \Monolog\Handler\RotatingFileHandler(__DIR__ . DIRECTORY_SEPARATOR . 'everything',3, \Monolog\Logger::DEBUG), ); - } + }, ); \ No newline at end of file