RavenHandler.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  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 Monolog\Formatter\LineFormatter;
  12. use Monolog\Formatter\FormatterInterface;
  13. use Monolog\Logger;
  14. use Monolog\Handler\AbstractProcessingHandler;
  15. use Raven_Client;
  16. /**
  17. * Handler to send messages to a Sentry (https://github.com/dcramer/sentry) server
  18. * using raven-php (https://github.com/getsentry/raven-php)
  19. *
  20. * @author Marc Abramowitz <marc@marc-abramowitz.com>
  21. */
  22. class RavenHandler extends AbstractProcessingHandler
  23. {
  24. /**
  25. * Translates Monolog log levels to Raven log levels.
  26. */
  27. private $logLevels = array(
  28. Logger::DEBUG => Raven_Client::DEBUG,
  29. Logger::INFO => Raven_Client::INFO,
  30. Logger::NOTICE => Raven_Client::INFO,
  31. Logger::WARNING => Raven_Client::WARNING,
  32. Logger::ERROR => Raven_Client::ERROR,
  33. Logger::CRITICAL => Raven_Client::FATAL,
  34. Logger::ALERT => Raven_Client::FATAL,
  35. Logger::EMERGENCY => Raven_Client::FATAL,
  36. );
  37. /**
  38. * @var Raven_Client the client object that sends the message to the server
  39. */
  40. protected $ravenClient;
  41. /**
  42. * @var LineFormatter The formatter to use for the logs generated via handleBatch()
  43. */
  44. protected $logFormatter;
  45. /**
  46. * @param Raven_Client $ravenClient
  47. * @param integer $level The minimum logging level at which this handler will be triggered
  48. * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
  49. */
  50. public function __construct(Raven_Client $ravenClient, $level = Logger::DEBUG, $bubble = true)
  51. {
  52. parent::__construct($level, $bubble);
  53. $this->ravenClient = $ravenClient;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function handleBatch(array $records)
  59. {
  60. // the last records is the "main" one
  61. $record = array_pop($records);
  62. // no need to go further if the main record is not at the right level
  63. if ($record['level'] < $this->level) {
  64. return;
  65. }
  66. // the other ones are added as a context item
  67. $logs = array();
  68. foreach ($records as $r) {
  69. if ($r['level'] < $this->level) {
  70. continue;
  71. }
  72. $logs[] = $this->processRecord($r);
  73. }
  74. if ($logs) {
  75. $record['context']['logs'] = (string) $this->getLogFormatter()->formatBatch($logs);
  76. }
  77. $this->handle($record);
  78. }
  79. /**
  80. * Sets the formatter for the logs generated by handleBatch().
  81. *
  82. * @param FormatterInterface $formatter
  83. */
  84. public function setLogFormatter(FormatterInterface $formatter)
  85. {
  86. $this->logFormatter = $formatter;
  87. }
  88. /**
  89. * Gets the formatter for the logs generated by handleBatch().
  90. *
  91. * @return FormatterInterface
  92. */
  93. public function getLogFormatter()
  94. {
  95. if (!$this->logFormatter) {
  96. $this->logFormatter = $this->getDefaultLogFormatter();
  97. }
  98. return $this->logFormatter;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. protected function write(array $record)
  104. {
  105. $options = array();
  106. $options['level'] = $this->logLevels[$record['level']];
  107. if (!empty($record['context'])) {
  108. $options['extra']['context'] = $record['context'];
  109. }
  110. if (!empty($record['extra'])) {
  111. $options['extra']['extra'] = $record['extra'];
  112. }
  113. if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Exception) {
  114. $options['extra']['message'] = $record['formatted'];
  115. $this->ravenClient->captureException($record['context']['exception'], $options);
  116. return;
  117. }
  118. $this->ravenClient->captureMessage($record['formatted'], array(), $options);
  119. }
  120. /**
  121. * {@inheritDoc}
  122. */
  123. protected function getDefaultFormatter()
  124. {
  125. return new LineFormatter('[%channel%] %message%');
  126. }
  127. /**
  128. * Gets the default formatter for the logs generated by handleBatch().
  129. *
  130. * @return FormatterInterface
  131. */
  132. protected function getDefaultLogFormatter()
  133. {
  134. return new LineFormatter();
  135. }
  136. }