RollbarHandler.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. * Rollbar's context info will contain the context + extra keys from the log record
  21. * merged, and then on top of that a few keys:
  22. *
  23. * - level (rollbar level name)
  24. * - monolog_level (monolog level name, raw level, as rollbar only has 5 but monolog 8)
  25. * - channel
  26. * - datetime (unix timestamp)
  27. *
  28. * @author Paul Statezny <paulstatezny@gmail.com>
  29. */
  30. class RollbarHandler extends AbstractProcessingHandler
  31. {
  32. /**
  33. * Rollbar notifier
  34. *
  35. * @var RollbarNotifier
  36. */
  37. protected $rollbarNotifier;
  38. protected $levelMap = [
  39. Logger::DEBUG => 'debug',
  40. Logger::INFO => 'info',
  41. Logger::NOTICE => 'info',
  42. Logger::WARNING => 'warning',
  43. Logger::ERROR => 'error',
  44. Logger::CRITICAL => 'critical',
  45. Logger::ALERT => 'critical',
  46. Logger::EMERGENCY => 'critical',
  47. ];
  48. /**
  49. * Records whether any log records have been added since the last flush of the rollbar notifier
  50. *
  51. * @var bool
  52. */
  53. private $hasRecords = false;
  54. /**
  55. * @param RollbarNotifier $rollbarNotifier RollbarNotifier object constructed with valid token
  56. * @param int $level The minimum logging level at which this handler will be triggered
  57. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  58. */
  59. public function __construct(RollbarNotifier $rollbarNotifier, $level = Logger::ERROR, $bubble = true)
  60. {
  61. $this->rollbarNotifier = $rollbarNotifier;
  62. parent::__construct($level, $bubble);
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. protected function write(array $record)
  68. {
  69. $context = $record['context'];
  70. $payload = [];
  71. if (isset($context['payload'])) {
  72. $payload = $context['payload'];
  73. unset($context['payload']);
  74. }
  75. $context = array_merge($context, $record['extra'], [
  76. 'level' => $this->levelMap[$record['level']],
  77. 'monolog_level' => $record['level_name'],
  78. 'channel' => $record['channel'],
  79. 'datetime' => $record['datetime']->format('U'),
  80. ]);
  81. if (isset($context['exception']) && $context['exception'] instanceof Exception) {
  82. $exception = $context['exception'];
  83. unset($context['exception']);
  84. $this->rollbarNotifier->report_exception($exception, $context, $payload);
  85. } else {
  86. $this->rollbarNotifier->report_message(
  87. $record['message'],
  88. $context['level'],
  89. $context,
  90. $payload
  91. );
  92. }
  93. $this->hasRecords = true;
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function close()
  99. {
  100. if ($this->hasRecords) {
  101. $this->rollbarNotifier->flush();
  102. $this->hasRecords = false;
  103. }
  104. }
  105. }