-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathRollbar.php
More file actions
587 lines (540 loc) · 18.2 KB
/
Copy pathRollbar.php
File metadata and controls
587 lines (540 loc) · 18.2 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
<?php declare(strict_types=1);
namespace Rollbar;
use Exception;
use Psr\Log\InvalidArgumentException;
use Rollbar\Payload\Level;
use Rollbar\Handlers\FatalHandler;
use Rollbar\Handlers\ErrorHandler;
use Rollbar\Handlers\ExceptionHandler;
use Rollbar\Payload\TelemetryBody;
use Rollbar\Payload\TelemetryEvent;
use Rollbar\Telemetry\EventLevel;
use Rollbar\Telemetry\EventType;
use Rollbar\Telemetry\Telemeter;
use Stringable;
use Throwable;
class Rollbar
{
/**
* The instance of the logger. This is null if Rollbar has not been initialized or {@see Rollbar::destroy()} has
* been called.
*
* @var RollbarLogger|null
*/
private static ?RollbarLogger $logger = null;
/**
* The fatal error handler instance or null if it was disabled or Rollbar has not been initialized.
*
* @var FatalHandler|null
*/
private static ?FatalHandler $fatalHandler = null;
/**
* The error handler instance or null if it was disabled or Rollbar has not been initialized.
*
* @var ErrorHandler|null
*/
private static ?ErrorHandler $errorHandler = null;
/**
* The exception handler instance or null if it was disabled or Rollbar has not been initialized.
*
* @var ExceptionHandler|null
*/
private static ?ExceptionHandler $exceptionHandler = null;
/**
* The instance of the telemeter. This is null if Rollbar has not been initialized or {@see Rollbar::destroy()} has
* been called.
*
* The Telemeter instance is placed here so that it is not destroyed and the queue lost if the Rollbar config is
* changed.
*
* @var Telemeter|null
*
* @since 4.1.0
*/
private static ?Telemeter $telemeter = null;
/**
* Sets up Rollbar monitoring and logging.
*
* This method may be called more than once to update or extend the configs. To do this pass an array as the
* $configOrLogger argument. Any config values in the array will update or replace existing configs.
*
* Note: this and the following two parameters are only used the first time the logger is created. This prevents
* multiple error monitors from reporting the same error more than once. To change these values you must call
* {@see Rollbar::destroy()} first.
*
* Example:
*
* // Turn off the fatal error handler
* $configs = Rollbar::logger()->getConfig();
* Rollbar::destroy();
* Rollbar::init($configs, handleFatal: false);
*
* @param RollbarLogger|array $configOrLogger This can be either an array of config options or an already
* configured {@see RollbarLogger} instance.
* @param bool $handleException If set to false Rollbar will not monitor exceptions.
* @param bool $handleError If set to false Rollbar will not monitor errors.
* @param bool $handleFatal If set to false Rollbar will not monitor fatal errors.
*
* @return void
* @throws Exception If the $configOrLogger argument is an array that has invalid configs.
*
* @link https://docs.rollbar.com/docs/basic-php-installation-setup
*/
public static function init(
RollbarLogger|array $configOrLogger,
bool $handleException = true,
bool $handleError = true,
bool $handleFatal = true
): void {
$setupHandlers = is_null(self::$logger);
self::setLogger($configOrLogger);
if ($setupHandlers) {
if ($handleException) {
self::setupExceptionHandling();
}
if ($handleError) {
self::setupErrorHandling();
}
if ($handleFatal) {
self::setupFatalHandling();
}
self::setupBatchHandling();
}
self::updateTelemeter();
}
/**
* Creates or configures the RollbarLogger instance.
*
* @param RollbarLogger|array $configOrLogger The configs array or a new {@see RollbarLogger} to use or replace the
* current one with, if one already exists. If a logger already exists
* and this is an array the current logger's configs will be extended
* configs from the array.
*
* @return void
* @throws Exception If the $configOrLogger argument is an array that has invalid configs.
*/
private static function setLogger(RollbarLogger|array $configOrLogger): void
{
if ($configOrLogger instanceof RollbarLogger) {
self::$logger = $configOrLogger;
self::updateTelemeter();
return;
}
// Replacing the logger rather than configuring the existing logger breaks BC
if (self::$logger !== null) {
self::$logger->configure($configOrLogger);
return;
}
self::$logger = new RollbarLogger($configOrLogger);
self::updateTelemeter();
}
/**
* Updates the telemeter instance with the latest configs.
*
* @return void
*
* @since 4.1.0
*/
private static function updateTelemeter(): void
{
self::$telemeter = self::$logger->getConfig()->getTelemetry(self::$telemeter);
}
/**
* Enables logging of errors to Rollbar.
*
* @return void
*/
public static function enable(): void
{
self::logger()->enable();
}
/**
* Disables logging of errors to Rollbar.
*
* @return void
*/
public static function disable(): void
{
self::logger()->disable();
}
/**
* Returns true if the Rollbar logger is enabled.
*
* @return bool
*/
public static function enabled(): bool
{
return self::logger()->enabled();
}
/**
* Returns true if the Rollbar logger is disabled.
*
* @return bool
*/
public static function disabled(): bool
{
return self::logger()->disabled();
}
/**
* Returns the current logger instance, or null if it has not been initialized or has been destroyed.
*
* @return RollbarLogger|null
*/
public static function logger(): ?RollbarLogger
{
return self::$logger;
}
/**
* Creates and returns a new {@see RollbarLogger} instance.
*
* @param array $config The configs extend the configs of the current logger instance, if it exists.
*
* @return RollbarLogger
* @throws Exception If the $config argument is an array that has invalid configs.
*/
public static function scope(array $config): RollbarLogger
{
if (is_null(self::$logger)) {
return new RollbarLogger($config);
}
$logger = self::$logger->scope($config);
// Reassign the telemeter in case the config changed.
self::updateTelemeter();
return $logger;
}
/**
* Logs a message to the Rollbar service with the specified level.
*
* @param Level|string $level The severity level of the message.
* Must be one of the levels as defined in
* the {@see Level} constants.
* @param string|Stringable $message The log message.
* @param array $context Arbitrary data.
*
* @return void
*
* @throws InvalidArgumentException If $level is not a valid level.
* @throws Throwable Rethrown $message if it is {@see Throwable} and {@see Config::raiseOnError} is true.
*/
public static function log($level, string|Stringable $message, array $context = array()): void
{
if (is_null(self::$logger)) {
return;
}
self::$logger->log($level, $message, $context);
}
/**
* Creates the {@see Response} object and reports the message to the Rollbar
* service.
*
* @param string|Level $level The severity level to send to Rollbar.
* @param string|Stringable $message The log message.
* @param array $context Any additional context data.
*
* @return Response
*
* @throws InvalidArgumentException If $level is not a valid level.
* @throws Throwable Rethrown $message if it is {@see Throwable} and {@see Config::raiseOnError} is true.
*
* @since 4.0.0
*/
public static function report($level, string|Stringable $message, array $context = array()): Response
{
if (is_null(self::$logger)) {
return self::getNotInitializedResponse();
}
return self::$logger->report($level, $message, $context);
}
/**
* Attempts to log a {@see Throwable} as an uncaught exception.
*
* @param string|Level $level The log level severity to use.
* @param Throwable $toLog The exception to log.
* @param array $context The array of additional data to pass with the stack trace.
*
* @return Response
* @throws Throwable The rethrown $toLog.
*
* @since 3.0.0
*/
public static function logUncaught(string|Level $level, Throwable $toLog, array $context = array()): Response
{
if (is_null(self::$logger)) {
return self::getNotInitializedResponse();
}
return self::$logger->report($level, $toLog, $context, isUncaught: true);
}
/**
* Logs a message with the {@see Level::DEBUG} log level.
*
* @param string|Stringable $message The debug message to log.
* @param array $context The additional data to send with the message.
*
* @return void
* @throws Throwable
*/
public static function debug(string|Stringable $message, array $context = array()): void
{
self::log(Level::DEBUG, $message, $context);
}
/**
* Logs a message with the {@see Level::INFO} log level.
*
* @param string|Stringable $message The info message to log.
* @param array $context The additional data to send with the message.
*
* @return void
* @throws Throwable
*/
public static function info(string|Stringable $message, array $context = array()): void
{
self::log(Level::INFO, $message, $context);
}
/**
* Logs a message with the {@see Level::NOTICE} log level.
*
* @param string|Stringable $message The notice message to log.
* @param array $context The additional data to send with the message.
*
* @return void
* @throws Throwable
*/
public static function notice(string|Stringable $message, array $context = array()): void
{
self::log(Level::NOTICE, $message, $context);
}
/**
* Logs a message with the {@see Level::WARNING} log level.
*
* @param string|Stringable $message The warning message to log.
* @param array $context The additional data to send with the message.
*
* @return void
* @throws Throwable
*/
public static function warning(string|Stringable $message, array $context = array()): void
{
self::log(Level::WARNING, $message, $context);
}
/**
* Logs a message with the {@see Level::ERROR} log level.
*
* @param string|Stringable $message The error message to log.
* @param array $context The additional data to send with the message.
*
* @return void
* @throws Throwable
*/
public static function error(string|Stringable $message, array $context = array()): void
{
self::log(Level::ERROR, $message, $context);
}
/**
* Logs a message with the {@see Level::CRITICAL} log level.
*
* @param string|Stringable $message The critical message to log.
* @param array $context The additional data to send with the message.
*
* @return void
* @throws Throwable
*/
public static function critical(string|Stringable $message, array $context = array()): void
{
self::log(Level::CRITICAL, $message, $context);
}
/**
* Logs a message with the {@see Level::ALERT} log level.
*
* @param string|Stringable $message The alert message to log.
* @param array $context The additional data to send with the message.
*
* @return void
* @throws Throwable
*/
public static function alert(string|Stringable $message, array $context = array()): void
{
self::log(Level::ALERT, $message, $context);
}
/**
* Logs a message with the {@see Level::EMERGENCY} log level.
*
* @param string|Stringable $message The emergency message to log.
* @param array $context The additional data to send with the message.
*
* @return void
* @throws Throwable
*/
public static function emergency(string|Stringable $message, array $context = array()): void
{
self::log(Level::EMERGENCY, $message, $context);
}
/**
* Captures a telemetry event that may be sent with future payloads.
*
* @param EventType $type The type of telemetry data
* @param EventLevel $level The severity level of the telemetry data.
* @param array|TelemetryBody $metadata Additional data about the telemetry event.
* @param string|null $uuid The Rollbar UUID to associate with this telemetry event.
* @param int|null $timestamp When this occurred, as a unix timestamp in milliseconds. If not provided,
* the current time will be used.
*
* @return TelemetryEvent|null Returns the {@see TelemetryEvent} that was captured or null if Rollbar or the
* {@see Telemeter} has not been initialized or the event is filtered out.
*
* @since 4.1.0
*/
public static function captureTelemetryEvent(
EventType $type,
EventLevel $level,
array|TelemetryBody $metadata,
?string $uuid = null,
?int $timestamp = null,
): ?TelemetryEvent {
if (is_null(self::$logger)) {
return null;
}
return self::$logger->captureTelemetryEvent($type, $level, $metadata, $uuid, $timestamp);
}
/**
* Creates a listener that monitors for exceptions.
*
* @return void
*/
public static function setupExceptionHandling(): void
{
self::$exceptionHandler = new ExceptionHandler(self::$logger);
self::$exceptionHandler->register();
}
/**
* Creates a listener that monitors for errors.
*
* @return void
*/
public static function setupErrorHandling(): void
{
self::$errorHandler = new ErrorHandler(self::$logger);
self::$errorHandler->register();
}
/**
* Creates a listener that monitors for fatal errors that cause the program to shut down.
*
* @return void
*/
public static function setupFatalHandling(): void
{
self::$fatalHandler = new FatalHandler(self::$logger);
self::$fatalHandler->register();
}
/**
* Returns the configured instance of the telemeter.
*
* @return Telemeter|null
*
* @since 4.1.0
*/
public static function getTelemeter(): ?Telemeter
{
return self::$telemeter;
}
/**
* Creates and returns a {@see Response} to use if Rollbar is attempted to be used prior to being initialized.
*
* @return Response
*/
private static function getNotInitializedResponse(): Response
{
return new Response(0, "Rollbar Not Initialized");
}
/**
* This method makes sure the queue of logs stored in memory are sent to Rollbar prior to shut down.
*
* @return void
*/
public static function setupBatchHandling(): void
{
register_shutdown_function('Rollbar\Rollbar::flushAndWait');
}
/**
* Sends all the queued logs to Rollbar.
*
* @return void
*/
public static function flush(): void
{
if (is_null(self::$logger)) {
return;
}
self::$logger->flush();
}
/**
* Sends all the queued logs to Rollbar and waits for the response.
*
* @return void
*/
public static function flushAndWait(): void
{
if (is_null(self::$logger)) {
return;
}
self::$logger->flushAndWait();
}
/**
* Adds a new key / value pair that will be sent with the payload to Rollbar. If the key already exists in the
* custom data array the existing value will be overwritten.
*
* @param string $key The key to store this value in the custom array.
* @param mixed $data The value that is going to be stored. Must be a primitive or JSON serializable.
*
* @return void
*/
public static function addCustom(string $key, mixed $data): void
{
self::$logger->addCustom($key, $data);
}
/**
* Removes a key from the custom data array that is sent with the payload to Rollbar.
*
* @param string $key The key to remove.
*
* @return void
*/
public static function removeCustom(string $key): void
{
self::$logger->removeCustom($key);
}
/**
* Returns the array of key / value pairs that will be sent with the payload to Rollbar.
*
* @return array|null
*/
public static function getCustom(): ?array
{
return self::$logger->getCustom();
}
/**
* Configures the existing {@see RollbarLogger} instance.
*
* @param array $config The array of configs. This does not need to be complete as it extends the existing
* configuration. Any existing values present in the new configs will be overwritten.
*
* @return void
*/
public static function configure(array $config): void
{
self::$logger->configure($config);
self::updateTelemeter();
}
/**
* Destroys the currently stored $logger and $telemeter allowing for a fresh configuration. This is especially used
* in testing scenarios.
*
* @return void
*
* @since 4.1.0 Also destroys the telemeter.
*/
public static function destroy(): void
{
self::$logger = null;
self::$telemeter = null;
}
}