-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollbar.module
235 lines (205 loc) · 6.05 KB
/
rollbar.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* @file Rollbar module main file.
*/
// include rollbar admin form functions
include_once 'rollbar.admin.inc';
/**
* Helper function to get the path to the library,
* since we include the Rollbar class at hook_boot, the modules
* like, libraries hasn't been loaded yet, so we imclude it manually
*/
function _rollbar_get_library_path() {
return variable_get('rollbar_lib_path', 'sites/all/libraries/rollbar/rollbar.php');
}
/**
* Implements hook_permission()
*/
function rollbar_permission() {
return array(
'administer rollbar' => array(
'title' => t('Administer rollbar settings'),
'description' => t('Allow user to administer all Rollbar settings.'),
),
);
}
/**
* Implements hook_requirements().
*/
function rollbar_requirements($phase) {
$requirements = array();
// only runtime, we dont care about installation or update
if ($phase == 'runtime') {
// make sure the rollbar class exists, we don't check for the existance
// of the class file here since it should always be included in hook_boot()
// which should already have been called by now. Since it's required on
// every page request
if (!class_exists('Rollbar')) {
$requirements['rollbar_library'] = array(
'title' => t('Rollbar library is missing'),
'description' => t(
'The path to the Rollbar library is missing, see the
README or adjust your settings !link',
array(
'!link' => l('here', 'admin/config/development/rollbar')
)
),
'value' => _rollbar_get_library_path(),
'severity' => REQUIREMENT_WARNING,
);
}
else {
$requirements['rollbar_library'] = array(
'title' => t('Rollbar library'),
'value' => RollbarNotifier::VERSION,
'severity' => REQUIREMENT_OK,
);
}
}
return $requirements;
}
/**
* Implements hook_boot().
*/
function rollbar_boot() {
// If php support is turned off then return
if (variable_get('rollbar_enable_php', 0) == 0) {
return;
}
// load rollbar settings
$access_token = variable_get('rollbar_server_access_token', '');
$environment = variable_get('rollbar_environment', 'production');
$exceptions = variable_get('rollbar_exceptions', 1);
$errors = variable_get('rollbar_error_handling', 1);
// get path to the library
$rollbar_lib_path = _rollbar_get_library_path();
// make sure that the file exists before inclusion
if (!file_exists($rollbar_lib_path)) {
watchdog(
'rollbar',
'Unable to load Rollbar library. File @path does not exist. Check configuration settings.',
array('@path' => $rollbar_lib_path),
WATCHDOG_NOTICE
);
return;
}
include_once $rollbar_lib_path;
Rollbar::init(
array(
'access_token' => $access_token,
'environment' => $environment
), $exceptions, $errors
);
}
/**
* Implements hook_watchdog().
*/
function rollbar_watchdog($log_entry) {
if (variable_get('rollbar_watchdog', 0) != 1) {
return;
}
// if this is a PHP error, and we haven't specifically choosen
// to frard those errors to rollbar then return
if ($log_entry['type'] == 'php') {
if (variable_get('rollbar_watchdog_include_php', '0') != 1) {
return;
}
}
switch ($log_entry['severity']) {
case WATCHDOG_ALERT:
case WATCHDOG_CRITICAL:
case WATCHDOG_EMERGENCY:
case WATCHDOG_ERROR:
$level = 'error';
break;
case WATCHDOG_WARNING:
$level = 'warning';
break;
case WATCHDOG_INFO:
case WATCHDOG_DEBUG:
case WATCHDOG_NOTICE:
$level = 'info';
break;
default:
$level = 'info';
break;
}
// ensure that the Rollbar class exists, this is an additional check
// in case the hook_boot hook was unable to locate the Rollbar class,
// this will happen if the user hasn't specified the correct path
if (class_exists('Rollbar')) {
Rollbar::report_message(
t($log_entry['message'], $log_entry['variables']),
$level,
array(
'type' => $log_entry['type'],
'uid' => $log_entry['uid'],
'request_uri' => $log_entry['request_uri'],
'referer' => $log_entry['referer'],
'ip' => $log_entry['ip']
)
);
}
}
/**
* Implements hook_init().
*/
function rollbar_init() {
// If php support is turned off then return
if (variable_get('rollbar_enable_js', 0) == 0) {
return;
}
// I had some issues appending this via jQuery and using Drupal.behaviors,
// so have opted to use javascript, in line with Rollbar.com documentation
$token = variable_get('rollbar_client_access_token', '');
$env = variable_get('rollbar_environment', 'production');
// rollbar js function
$js = <<<"ROLLBAR_JS"
var _rollbarParams = {'server.environment': "$env"};
_rollbarParams['notifier.snippet_version'] = '2';
var _rollbar=["$token", _rollbarParams];
var _ratchet=_rollbar;
(function(w,d) {
w.onerror=function(e,u,l) {
_rollbar.push({_t:'uncaught',e:e,u:u,l:l});
};
var i=function(){
var s=d.createElement('script');
var f=d.getElementsByTagName('script')[0];
s.src='//d37gvrvc0wt4s1.cloudfront.net/js/1/rollbar.min.js';
s.async=!0;
f.parentNode.insertBefore(s,f);
};
if(w.addEventListener){
w.addEventListener('load',i,!1);
}else{
w.attachEvent('onload',i);
}
})(window,document);
ROLLBAR_JS;
// Add the function, we add it after drupal
// and jquery have been loaded.
// but before everything else.
drupal_add_js($js, array(
'type' => 'inline',
'scope' => 'header',
'weight' => -99,
'group' => JS_LIBRARY
));
}
/**
* Implements hook_menu().
*/
function rollbar_menu() {
$items = array();
$items['admin/config/development/rollbar'] = array(
'title' => 'Rollbar settings',
'description' => 'Make changes to your Rollbar settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('rollbar_admin_settings_form'),
'access arguments' => array('administer rollbar'),
'type' => MENU_NORMAL_ITEM,
'file' => 'rollbar.admin.inc',
);
return $items;
}