diff --git a/.editorconfig b/.editorconfig index 5599209..10dd286 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,10 +8,7 @@ root = true [*] end_of_line = LF indent_style = space -indent_size = 4 +indent_size = 2 charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true - -[{*.yml,*yaml}] -indent_size = 2 diff --git a/.gitignore b/.gitignore index fc2e41c..8193579 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ composer.lock .bash_history /.phpunit.result.cache .DS_Store +/.idea diff --git a/CHANGELOG b/CHANGELOG index e69de29..bbeb4b6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -0,0 +1,14 @@ +# simplesamlphp-module-rollbar changelog +All notable changes to simplesamlphp-module-rollbar will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [unreleased] +- + +## [v1.0.1] +- Add exception handling as an alternative to logger. + +## [v1.0.0] +- Initial release. diff --git a/README.md b/README.md index 4ec1cce..e77add2 100644 --- a/README.md +++ b/README.md @@ -2,16 +2,26 @@ SimpleSAMLphp + Rollbar -### simpleSAMLphp module +### SimpleSAMLphp module This module for SimpleSAMLphp provides a LoggerHandler that integrates with Rollbar service. ## Installation -1. Install simpleSAMLphp. -3. Install rollbar - `composer require systemseed/simplesamlphp-module-rollbar`. -4. Set `rollbar.token` value in `simplesamlphp/config/config.php`. -4. Set `rollbar.environment` value in `simplesamlphp/config/config.php`. -5. Set `logging.handler` to `'SimpleSAML\Module\rollbar\Logger\RollbarLoggingHandler'`. -6. Optionally set `logging.level` to `SimpleSAML\Logger::ERR` to send only errors. +- Install and configure SimpleSAMLphp. +- Install Rollbar - `composer require systemseed/simplesamlphp-module-rollbar`. +## Enable Rollbar + +- Fill `rollbar.token` value in `simplesamlphp/config/config.php` with server token taken from Rollbar. +- Set `rollbar.environment` value in `simplesamlphp/config/config.php` to define current environment name. + +### Configure Rollbar to handle PHP exceptions + +Set `rollbar.exception_handler` value to `true` in `simplesamlphp/config/config.php`. This will catch and log all +exceptions to Rollbar as a single occurrence in Rollbar dashboard. + +### Configure Rollbar to handle all log items + +Set `logging.handler` to `'SimpleSAML\Module\rollbar\Logger\RollbarLoggingHandler'`. Please be aware that enabled +backtraces, will result in each line in the backtrace treated as a separate occurrence. diff --git a/composer.json b/composer.json index de12a68..b3a78b1 100644 --- a/composer.json +++ b/composer.json @@ -27,14 +27,16 @@ "simplesamlphp/composer-module-installer": "~1.0" }, "require-dev": { - "squizlabs/php_codesniffer": "^3.0" + "squizlabs/php_codesniffer": "^3.0", + "drupal/coder": "8.3.13" }, "autoload-dev": { - "classmap": ["src/", "tests/"] + "classmap": ["src/"] }, "config": { "allow-plugins": { - "simplesamlphp/composer-module-installer": true + "simplesamlphp/composer-module-installer": true, + "dealerdirect/phpcodesniffer-composer-installer": true } } } diff --git a/hooks/hook_exception_handler.php b/hooks/hook_exception_handler.php new file mode 100644 index 0000000..2f27a06 --- /dev/null +++ b/hooks/hook_exception_handler.php @@ -0,0 +1,40 @@ +getOptionalBoolean('rollbar.exception_handler', FALSE); + $token = $config->getOptionalString('rollbar.token', ''); + $environment = $config->getOptionalString('rollbar.environment', ''); + + if ($enabled && $token && $environment) { + // Initialize and configure the logger. + Rollbar::init([ + 'access_token' => $token, + 'environment' => $environment, + ], FALSE, FALSE, FALSE); + + // Log the exception. + Rollbar::logger()->report(Level::ERROR, $exception, isUncaught: TRUE); + } +} diff --git a/phpcs.xml b/phpcs.xml index a6a180f..5a49ae9 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,8 +1,15 @@ - - - The coding standard for rollbar. + + + Drupal based codestyle ruleset - - ./src - ./public + + + + + + + + + ./hooks + ./src diff --git a/src/Logger/RollbarLoggingHandler.php b/src/Logger/RollbarLoggingHandler.php index 5b6c0a2..ae4f8df 100644 --- a/src/Logger/RollbarLoggingHandler.php +++ b/src/Logger/RollbarLoggingHandler.php @@ -13,123 +13,120 @@ * * @package SimpleSAMLphp */ -class RollbarLoggingHandler implements LoggingHandlerInterface -{ - /** - * Checks if the Rollbar is initialized. - * - * @var bool - */ - private $isInitialized = false; - - /** - * This array contains the mappings from syslog log level to names. - * - * @var array - */ - private static array $levelMap = [ - Logger::EMERG => RollbarLogLevel::EMERGENCY, - Logger::ALERT => RollbarLogLevel::ALERT, - Logger::CRIT => RollbarLogLevel::CRITICAL, - Logger::ERR => RollbarLogLevel::ERROR, - Logger::WARNING => RollbarLogLevel::WARNING, - Logger::NOTICE => RollbarLogLevel::NOTICE, - Logger::INFO => RollbarLogLevel::INFO, - Logger::DEBUG => RollbarLogLevel::DEBUG, - ]; - - /** - * The name of this process. - * - * @var string - */ - private string $processname; - - /** - * The Rollbar API token. - * - * @var string - */ - private string $token; - - /** - * The name of this envrionment. - * - * @var string - */ - private string $environment; - - - /** - * ErrorLogLoggingHandler constructor. - * - * @param \SimpleSAML\Configuration $config The configuration object for this handler. - */ - public function __construct(Configuration $config) - { - // Remove any non-printable characters before storing - $this->processname = preg_replace( - '/[\x00-\x1F\x7F\xA0]/u', - '', - $config->getOptionalString('logging.processname', 'SimpleSAMLphp') - ); - - // Rollbar related configs. - $this->token = $config->getOptionalString('rollbar.token', ''); - $this->environment = $config->getOptionalString('rollbar.environment', ''); +class RollbarLoggingHandler implements LoggingHandlerInterface { + /** + * Checks if the Rollbar is initialized. + * + * @var bool + */ + private $isInitialized = FALSE; + + /** + * This array contains the mappings from syslog log level to names. + * + * @var arrayintstring + */ + private static array $levelMap = [ + Logger::EMERG => RollbarLogLevel::EMERGENCY, + Logger::ALERT => RollbarLogLevel::ALERT, + Logger::CRIT => RollbarLogLevel::CRITICAL, + Logger::ERR => RollbarLogLevel::ERROR, + Logger::WARNING => RollbarLogLevel::WARNING, + Logger::NOTICE => RollbarLogLevel::NOTICE, + Logger::INFO => RollbarLogLevel::INFO, + Logger::DEBUG => RollbarLogLevel::DEBUG, + ]; + + /** + * The name of this process. + * + * @var string + */ + private string $processname; + + /** + * The Rollbar API token. + * + * @var string + */ + private string $token; + + /** + * The name of this envrionment. + * + * @var string + */ + private string $environment; + + /** + * ErrorLogLoggingHandler constructor. + * + * @param \SimpleSAML\Configuration $config + * The configuration object for this handler. + */ + public function __construct(Configuration $config) { + // Remove any non-printable characters before storing. + $this->processname = preg_replace( + '/[\x00-\x1F\x7F\xA0]/u', + '', + $config->getOptionalString('logging.processname', 'SimpleSAMLphp') + ); + + // Rollbar related configs. + $this->token = $config->getOptionalString('rollbar.token', ''); + $this->environment = $config->getOptionalString('rollbar.environment', ''); + } + + /** + * Initialize rollbar object. + */ + protected function init() { + if (empty($this->token) || empty($this->environment)) { + return FALSE; } - /** - * Initialize rollbar object. - */ - protected function init() - { - if (empty($this->token) || empty($this->environment)) { - return false; - } - - if (!$this->isInitialized) { - Rollbar::init([ - 'access_token' => $this->token, - 'environment' => $this->environment, - ]); - $this->isInitialized = true; - } - - return true; + if (!$this->isInitialized) { + Rollbar::init([ + 'access_token' => $this->token, + 'environment' => $this->environment, + ]); + $this->isInitialized = TRUE; } - - /** - * Set the format desired for the logs. - * - * @param string $format The format used for logs. - */ - public function setLogFormat(string $format): void - { - // we don't need the format here + return TRUE; + } + + /** + * Set the format desired for the logs. + * + * @param string $format + * The format used for logs. + */ + public function setLogFormat(string $format): void { + // We don't need the format here. + } + + /** + * Log a message to syslog. + * + * @param int $level + * The log level. + * @param string $string + * The formatted message to log. + */ + public function log(int $level, string $string): void { + $levelName = self::$levelMap[$level] ?? sprintf('UNKNOWN%d', $level); + if (!$this->init()) { + return; } + $formats = ['%process', '%level']; + $replacements = [$this->processname, strtoupper($levelName)]; + $string = str_replace($formats, $replacements, $string); + $string = preg_replace('/^%date(\{[^\}]+\})?\s*/', '', $string); + $string = trim($string); + + Rollbar::log($levelName, $string); + } - /** - * Log a message to syslog. - * - * @param int $level The log level. - * @param string $string The formatted message to log. - */ - public function log(int $level, string $string): void - { - $levelName = self::$levelMap[$level] ?? sprintf('UNKNOWN%d', $level); - if (!$this->init()) { - return; - } - - $formats = ['%process', '%level']; - $replacements = [$this->processname, strtoupper($levelName)]; - $string = str_replace($formats, $replacements, $string); - $string = preg_replace('/^%date(\{[^\}]+\})?\s*/', '', $string); - $string = trim($string); - - Rollbar::log($levelName, $string); - } }