RollbarHandler.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 Rollbar\RollbarLogger;
  12. use Throwable;
  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 RollbarLogger's log method.
  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. * @var RollbarLogger
  34. */
  35. protected $rollbarLogger;
  36. protected $levelMap = [
  37. Logger::DEBUG => 'debug',
  38. Logger::INFO => 'info',
  39. Logger::NOTICE => 'info',
  40. Logger::WARNING => 'warning',
  41. Logger::ERROR => 'error',
  42. Logger::CRITICAL => 'critical',
  43. Logger::ALERT => 'critical',
  44. Logger::EMERGENCY => 'critical',
  45. ];
  46. /**
  47. * Records whether any log records have been added since the last flush of the rollbar notifier
  48. *
  49. * @var bool
  50. */
  51. private $hasRecords = false;
  52. protected $initialized = false;
  53. /**
  54. * @param RollbarLogger $rollbarLogger RollbarLogger object constructed with valid token
  55. * @param string|int $level The minimum logging level at which this handler will be triggered
  56. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  57. */
  58. public function __construct(RollbarLogger $rollbarLogger, $level = Logger::ERROR, bool $bubble = true)
  59. {
  60. $this->rollbarLogger = $rollbarLogger;
  61. parent::__construct($level, $bubble);
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. protected function write(array $record): void
  67. {
  68. if (!$this->initialized) {
  69. // __destructor() doesn't get called on Fatal errors
  70. register_shutdown_function(array($this, 'close'));
  71. $this->initialized = true;
  72. }
  73. $context = $record['context'];
  74. $context = array_merge($context, $record['extra'], [
  75. 'level' => $this->levelMap[$record['level']],
  76. 'monolog_level' => $record['level_name'],
  77. 'channel' => $record['channel'],
  78. 'datetime' => $record['datetime']->format('U'),
  79. ]);
  80. if (isset($context['exception']) && $context['exception'] instanceof Throwable) {
  81. $exception = $context['exception'];
  82. unset($context['exception']);
  83. $toLog = $exception;
  84. } else {
  85. $toLog = $record['message'];
  86. }
  87. $this->rollbarLogger->log($context['level'], $toLog, $context);
  88. $this->hasRecords = true;
  89. }
  90. public function flush(): void
  91. {
  92. if ($this->hasRecords) {
  93. $this->rollbarLogger->flush();
  94. $this->hasRecords = false;
  95. }
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function close(): void
  101. {
  102. $this->flush();
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function reset()
  108. {
  109. $this->flush();
  110. parent::reset();
  111. }
  112. }