RollbarHandler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use RollbarNotifier;
  12. use Exception;
  13. use Monolog\Logger;
  14. /**
  15. * Sends errors to Rollbar
  16. *
  17. * If the context data contains a `payload` key, that is used as an array
  18. * of payload options to RollbarNotifier's report_message/report_exception methods.
  19. *
  20. * @author Paul Statezny <paulstatezny@gmail.com>
  21. */
  22. class RollbarHandler extends AbstractProcessingHandler
  23. {
  24. /**
  25. * Rollbar notifier
  26. *
  27. * @var RollbarNotifier
  28. */
  29. protected $rollbarNotifier;
  30. /**
  31. * Records whether any log records have been added since the last flush of the rollbar notifier
  32. *
  33. * @var bool
  34. */
  35. private $hasRecords = false;
  36. /**
  37. * @param RollbarNotifier $rollbarNotifier RollbarNotifier object constructed with valid token
  38. * @param int $level The minimum logging level at which this handler will be triggered
  39. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  40. */
  41. public function __construct(RollbarNotifier $rollbarNotifier, $level = Logger::ERROR, $bubble = true)
  42. {
  43. $this->rollbarNotifier = $rollbarNotifier;
  44. parent::__construct($level, $bubble);
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function write(array $record)
  50. {
  51. if (isset($record['context']['exception']) && $record['context']['exception'] instanceof Exception) {
  52. $context = $record['context'];
  53. $exception = $context['exception'];
  54. unset($context['exception']);
  55. $payload = [];
  56. if (isset($context['payload'])) {
  57. $payload = $context['payload'];
  58. unset($context['payload']);
  59. }
  60. $this->rollbarNotifier->report_exception($exception, $context, $payload);
  61. } else {
  62. $extraData = [
  63. 'level' => $record['level'],
  64. 'channel' => $record['channel'],
  65. 'datetime' => $record['datetime']->format('U'),
  66. ];
  67. $context = $record['context'];
  68. $payload = [];
  69. if (isset($context['payload'])) {
  70. $payload = $context['payload'];
  71. unset($context['payload']);
  72. }
  73. $this->rollbarNotifier->report_message(
  74. $record['message'],
  75. $record['level_name'],
  76. array_merge($record['context'], $record['extra'], $extraData),
  77. $payload
  78. );
  79. }
  80. $this->hasRecords = true;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function close()
  86. {
  87. if ($this->hasRecords) {
  88. $this->rollbarNotifier->flush();
  89. $this->hasRecords = false;
  90. }
  91. }
  92. }