123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- use Monolog\Logger;
- use Monolog\Handler\AbstractProcessingHandler;
- class Raven_Breadcrumbs_MonologHandler extends AbstractProcessingHandler
- {
- /**
- * Translates Monolog log levels to Raven log levels.
- */
- protected $logLevels = array(
- Logger::DEBUG => Raven_Client::DEBUG,
- Logger::INFO => Raven_Client::INFO,
- Logger::NOTICE => Raven_Client::INFO,
- Logger::WARNING => Raven_Client::WARNING,
- Logger::ERROR => Raven_Client::ERROR,
- Logger::CRITICAL => Raven_Client::FATAL,
- Logger::ALERT => Raven_Client::FATAL,
- Logger::EMERGENCY => Raven_Client::FATAL,
- );
- protected $excMatch = '/^exception \'([^\']+)\' with message \'(.+)\' in .+$/s';
- /**
- * @var Raven_Client the client object that sends the message to the server
- */
- protected $ravenClient;
- /**
- * @param Raven_Client $ravenClient
- * @param int $level The minimum logging level at which this handler will be triggered
- * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
- */
- public function __construct(Raven_Client $ravenClient, $level = Logger::DEBUG, $bubble = true)
- {
- parent::__construct($level, $bubble);
- $this->ravenClient = $ravenClient;
- }
- /**
- * @param string $message
- * @return array|null
- */
- protected function parseException($message)
- {
- if (preg_match($this->excMatch, $message, $matches)) {
- return array($matches[1], $matches[2]);
- }
- return null;
- }
- /**
- * {@inheritdoc}
- */
- protected function write(array $record)
- {
- // sentry uses the 'nobreadcrumb' attribute to skip reporting
- if (!empty($record['context']['nobreadcrumb'])) {
- return;
- }
- if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Exception) {
- /**
- * @var Exception $exc
- */
- $exc = $record['context']['exception'];
- $crumb = array(
- 'type' => 'error',
- 'level' => $this->logLevels[$record['level']],
- 'category' => $record['channel'],
- 'data' => array(
- 'type' => get_class($exc),
- 'value' => $exc->getMessage(),
- ),
- );
- } else {
- // TODO(dcramer): parse exceptions out of messages and format as above
- if ($error = $this->parseException($record['message'])) {
- $crumb = array(
- 'type' => 'error',
- 'level' => $this->logLevels[$record['level']],
- 'category' => $record['channel'],
- 'data' => array(
- 'type' => $error[0],
- 'value' => $error[1],
- ),
- );
- } else {
- $crumb = array(
- 'level' => $this->logLevels[$record['level']],
- 'category' => $record['channel'],
- 'message' => $record['message'],
- );
- }
- }
- $this->ravenClient->breadcrumbs->record($crumb);
- }
- }
|