Skip to content

Commit 899cc3b

Browse files
committed
Add exception handler.
1 parent 3799feb commit 899cc3b

8 files changed

Lines changed: 200 additions & 132 deletions

File tree

.editorconfig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ root = true
88
[*]
99
end_of_line = LF
1010
indent_style = space
11-
indent_size = 4
11+
indent_size = 2
1212
charset = utf-8
1313
trim_trailing_whitespace = true
1414
insert_final_newline = true
15-
16-
[{*.yml,*yaml}]
17-
indent_size = 2

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ composer.lock
44
.bash_history
55
/.phpunit.result.cache
66
.DS_Store
7+
/.idea

CHANGELOG

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# simplesamlphp-module-rollbar changelog
2+
All notable changes to simplesamlphp-module-rollbar will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## [unreleased]
8+
-
9+
10+
## [v1.0.1]
11+
- Add exception handling as an alternative to logger.
12+
13+
## [v1.0.0]
14+
- Initial release.

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,26 @@
22

33
SimpleSAMLphp + Rollbar
44

5-
### simpleSAMLphp module
5+
### SimpleSAMLphp module
66

77
This module for SimpleSAMLphp provides a LoggerHandler that integrates with Rollbar service.
88

99
## Installation
1010

11-
1. Install simpleSAMLphp.
12-
3. Install rollbar - `composer require systemseed/simplesamlphp-module-rollbar`.
13-
4. Set `rollbar.token` value in `simplesamlphp/config/config.php`.
14-
4. Set `rollbar.environment` value in `simplesamlphp/config/config.php`.
15-
5. Set `logging.handler` to `'SimpleSAML\Module\rollbar\Logger\RollbarLoggingHandler'`.
16-
6. Optionally set `logging.level` to `SimpleSAML\Logger::ERR` to send only errors.
11+
- Install and configure SimpleSAMLphp.
12+
- Install Rollbar - `composer require systemseed/simplesamlphp-module-rollbar`.
1713

14+
## Enable Rollbar
15+
16+
- Fill `rollbar.token` value in `simplesamlphp/config/config.php` with server token taken from Rollbar.
17+
- Set `rollbar.environment` value in `simplesamlphp/config/config.php` to define current environment name.
18+
19+
### Configure Rollbar to handle PHP exceptions
20+
21+
Set `rollbar.exception_handler` value to `true` in `simplesamlphp/config/config.php`. This will catch and log all
22+
exceptions to Rollbar as a single occurrence in Rollbar dashboard.
23+
24+
### Configure Rollbar to handle all log items
25+
26+
Set `logging.handler` to `'SimpleSAML\Module\rollbar\Logger\RollbarLoggingHandler'`. Please be aware that enabled
27+
backtraces, will result in each line in the backtrace treated as a separate occurrence.

composer.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@
2727
"simplesamlphp/composer-module-installer": "~1.0"
2828
},
2929
"require-dev": {
30-
"squizlabs/php_codesniffer": "^3.0"
30+
"squizlabs/php_codesniffer": "^3.0",
31+
"drupal/coder": "8.3.13"
3132
},
3233
"autoload-dev": {
33-
"classmap": ["src/", "tests/"]
34+
"classmap": ["src/"]
3435
},
3536
"config": {
3637
"allow-plugins": {
37-
"simplesamlphp/composer-module-installer": true
38+
"simplesamlphp/composer-module-installer": true,
39+
"dealerdirect/phpcodesniffer-composer-installer": true
3840
}
3941
}
4042
}

hooks/hook_exception_handler.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Utilize hook provided by SimpleSAMLphp to log PHP exceptions.
6+
* @see https://simplesamlphp.org/docs/stable/simplesamlphp-modules.html#hook-interface
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
use Rollbar\Payload\Level;
12+
use Rollbar\Rollbar;
13+
use SimpleSAML\Configuration;
14+
15+
/**
16+
* Hook to run a cron job.
17+
*
18+
* @param Throwable $exception
19+
* The exception.
20+
*
21+
* @throws Throwable
22+
*/
23+
function rollbar_hook_exception_handler(Throwable $exception): void {
24+
// Check if Rollbar is configured to handle exceptions.
25+
$config = Configuration::getConfig();
26+
$enabled = $config->getOptionalBoolean('rollbar.exception_handler', FALSE);
27+
$token = $config->getOptionalString('rollbar.token', '');
28+
$environment = $config->getOptionalString('rollbar.environment', '');
29+
30+
if ($enabled && $token && $environment) {
31+
// Initialize and configure the logger.
32+
Rollbar::init([
33+
'access_token' => $token,
34+
'environment' => $environment,
35+
], FALSE, FALSE, FALSE);
36+
37+
// Log the exception.
38+
Rollbar::logger()->report(Level::ERROR, $exception, isUncaught: TRUE);
39+
}
40+
}

phpcs.xml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
<?xml version="1.0"?>
2-
<ruleset name="rollbar">
3-
<description>The coding standard for rollbar.</description>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ruleset name="phpcs-standard">
3+
<description>Drupal based codestyle ruleset</description>
44

5-
<rule ref="PSR12" />
6-
<file>./src</file>
7-
<file>./public</file>
5+
<!-- Specify standards. -->
6+
<rule ref="Drupal"/>
7+
<rule ref="DrupalPractice"/>
8+
9+
<!-- Include path with the Drupal and DrupalPractice rules. -->
10+
<config name="installed_paths" value="vendor/drupal/coder/coder_sniffer"/>
11+
12+
<!-- Specify folders. -->
13+
<file>./hooks</file>
14+
<file>./src</file>
815
</ruleset>

src/Logger/RollbarLoggingHandler.php

Lines changed: 109 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -13,123 +13,120 @@
1313
*
1414
* @package SimpleSAMLphp
1515
*/
16-
class RollbarLoggingHandler implements LoggingHandlerInterface
17-
{
18-
/**
19-
* Checks if the Rollbar is initialized.
20-
*
21-
* @var bool
22-
*/
23-
private $isInitialized = false;
24-
25-
/**
26-
* This array contains the mappings from syslog log level to names.
27-
*
28-
* @var array<int, string>
29-
*/
30-
private static array $levelMap = [
31-
Logger::EMERG => RollbarLogLevel::EMERGENCY,
32-
Logger::ALERT => RollbarLogLevel::ALERT,
33-
Logger::CRIT => RollbarLogLevel::CRITICAL,
34-
Logger::ERR => RollbarLogLevel::ERROR,
35-
Logger::WARNING => RollbarLogLevel::WARNING,
36-
Logger::NOTICE => RollbarLogLevel::NOTICE,
37-
Logger::INFO => RollbarLogLevel::INFO,
38-
Logger::DEBUG => RollbarLogLevel::DEBUG,
39-
];
40-
41-
/**
42-
* The name of this process.
43-
*
44-
* @var string
45-
*/
46-
private string $processname;
47-
48-
/**
49-
* The Rollbar API token.
50-
*
51-
* @var string
52-
*/
53-
private string $token;
54-
55-
/**
56-
* The name of this envrionment.
57-
*
58-
* @var string
59-
*/
60-
private string $environment;
61-
62-
63-
/**
64-
* ErrorLogLoggingHandler constructor.
65-
*
66-
* @param \SimpleSAML\Configuration $config The configuration object for this handler.
67-
*/
68-
public function __construct(Configuration $config)
69-
{
70-
// Remove any non-printable characters before storing
71-
$this->processname = preg_replace(
72-
'/[\x00-\x1F\x7F\xA0]/u',
73-
'',
74-
$config->getOptionalString('logging.processname', 'SimpleSAMLphp')
75-
);
76-
77-
// Rollbar related configs.
78-
$this->token = $config->getOptionalString('rollbar.token', '');
79-
$this->environment = $config->getOptionalString('rollbar.environment', '');
16+
class RollbarLoggingHandler implements LoggingHandlerInterface {
17+
/**
18+
* Checks if the Rollbar is initialized.
19+
*
20+
* @var bool
21+
*/
22+
private $isInitialized = FALSE;
23+
24+
/**
25+
* This array contains the mappings from syslog log level to names.
26+
*
27+
* @var arrayintstring
28+
*/
29+
private static array $levelMap = [
30+
Logger::EMERG => RollbarLogLevel::EMERGENCY,
31+
Logger::ALERT => RollbarLogLevel::ALERT,
32+
Logger::CRIT => RollbarLogLevel::CRITICAL,
33+
Logger::ERR => RollbarLogLevel::ERROR,
34+
Logger::WARNING => RollbarLogLevel::WARNING,
35+
Logger::NOTICE => RollbarLogLevel::NOTICE,
36+
Logger::INFO => RollbarLogLevel::INFO,
37+
Logger::DEBUG => RollbarLogLevel::DEBUG,
38+
];
39+
40+
/**
41+
* The name of this process.
42+
*
43+
* @var string
44+
*/
45+
private string $processname;
46+
47+
/**
48+
* The Rollbar API token.
49+
*
50+
* @var string
51+
*/
52+
private string $token;
53+
54+
/**
55+
* The name of this envrionment.
56+
*
57+
* @var string
58+
*/
59+
private string $environment;
60+
61+
/**
62+
* ErrorLogLoggingHandler constructor.
63+
*
64+
* @param \SimpleSAML\Configuration $config
65+
* The configuration object for this handler.
66+
*/
67+
public function __construct(Configuration $config) {
68+
// Remove any non-printable characters before storing.
69+
$this->processname = preg_replace(
70+
'/[\x00-\x1F\x7F\xA0]/u',
71+
'',
72+
$config->getOptionalString('logging.processname', 'SimpleSAMLphp')
73+
);
74+
75+
// Rollbar related configs.
76+
$this->token = $config->getOptionalString('rollbar.token', '');
77+
$this->environment = $config->getOptionalString('rollbar.environment', '');
78+
}
79+
80+
/**
81+
* Initialize rollbar object.
82+
*/
83+
protected function init() {
84+
if (empty($this->token) || empty($this->environment)) {
85+
return FALSE;
8086
}
8187

82-
/**
83-
* Initialize rollbar object.
84-
*/
85-
protected function init()
86-
{
87-
if (empty($this->token) || empty($this->environment)) {
88-
return false;
89-
}
90-
91-
if (!$this->isInitialized) {
92-
Rollbar::init([
93-
'access_token' => $this->token,
94-
'environment' => $this->environment,
95-
]);
96-
$this->isInitialized = true;
97-
}
98-
99-
return true;
88+
if (!$this->isInitialized) {
89+
Rollbar::init([
90+
'access_token' => $this->token,
91+
'environment' => $this->environment,
92+
]);
93+
$this->isInitialized = TRUE;
10094
}
10195

102-
103-
/**
104-
* Set the format desired for the logs.
105-
*
106-
* @param string $format The format used for logs.
107-
*/
108-
public function setLogFormat(string $format): void
109-
{
110-
// we don't need the format here
96+
return TRUE;
97+
}
98+
99+
/**
100+
* Set the format desired for the logs.
101+
*
102+
* @param string $format
103+
* The format used for logs.
104+
*/
105+
public function setLogFormat(string $format): void {
106+
// We don't need the format here.
107+
}
108+
109+
/**
110+
* Log a message to syslog.
111+
*
112+
* @param int $level
113+
* The log level.
114+
* @param string $string
115+
* The formatted message to log.
116+
*/
117+
public function log(int $level, string $string): void {
118+
$levelName = self::$levelMap[$level] ?? sprintf('UNKNOWN%d', $level);
119+
if (!$this->init()) {
120+
return;
111121
}
112122

123+
$formats = ['%process', '%level'];
124+
$replacements = [$this->processname, strtoupper($levelName)];
125+
$string = str_replace($formats, $replacements, $string);
126+
$string = preg_replace('/^%date(\{[^\}]+\})?\s*/', '', $string);
127+
$string = trim($string);
128+
129+
Rollbar::log($levelName, $string);
130+
}
113131

114-
/**
115-
* Log a message to syslog.
116-
*
117-
* @param int $level The log level.
118-
* @param string $string The formatted message to log.
119-
*/
120-
public function log(int $level, string $string): void
121-
{
122-
$levelName = self::$levelMap[$level] ?? sprintf('UNKNOWN%d', $level);
123-
if (!$this->init()) {
124-
return;
125-
}
126-
127-
$formats = ['%process', '%level'];
128-
$replacements = [$this->processname, strtoupper($levelName)];
129-
$string = str_replace($formats, $replacements, $string);
130-
$string = preg_replace('/^%date(\{[^\}]+\})?\s*/', '', $string);
131-
$string = trim($string);
132-
133-
Rollbar::log($levelName, $string);
134-
}
135132
}

0 commit comments

Comments
 (0)