You are here

public function Raven::log in Raven: Sentry Integration 8.2

Same name and namespace in other branches
  1. 8 src/Logger/Raven.php \Drupal\raven\Logger\Raven::log()
  2. 3.x src/Logger/Raven.php \Drupal\raven\Logger\Raven::log()

Overrides RfcLoggerTrait::log

File

src/Logger/Raven.php, line 211

Class

Raven
Logs events to Sentry.

Namespace

Drupal\raven\Logger

Code

public function log($level, $message, array $context = []) {
  if (!$this->client) {

    // Sad raven.
    return;
  }
  $levels = [
    RfcLogLevel::EMERGENCY => \Raven_Client::FATAL,
    RfcLogLevel::ALERT => \Raven_Client::FATAL,
    RfcLogLevel::CRITICAL => \Raven_Client::FATAL,
    RfcLogLevel::ERROR => \Raven_Client::ERROR,
    RfcLogLevel::WARNING => \Raven_Client::WARNING,
    RfcLogLevel::NOTICE => \Raven_Client::INFO,
    RfcLogLevel::INFO => \Raven_Client::INFO,
    RfcLogLevel::DEBUG => \Raven_Client::DEBUG,
  ];
  $data['level'] = $levels[$level];
  $message_placeholders = $this->parser
    ->parseMessagePlaceholders($message, $context);
  $formatted_message = empty($message_placeholders) ? $message : strtr($message, $message_placeholders);
  if ($message_limit = $this->config
    ->get('message_limit')) {
    $formatted_message = Unicode::truncate($formatted_message, $message_limit, FALSE, TRUE);
  }
  $data['sentry.interfaces.Message'] = [
    'message' => $message,
    'params' => $message_placeholders,
    'formatted' => $formatted_message,
  ];
  $data['timestamp'] = gmdate('Y-m-d\\TH:i:s\\Z', $context['timestamp']);
  $data['logger'] = $context['channel'];
  $data['extra']['link'] = (string) $context['link'];
  $data['extra']['referer'] = $context['referer'];
  $data['extra']['request_uri'] = $context['request_uri'];
  $data['user']['id'] = $context['uid'];
  $data['user']['ip_address'] = $context['ip'];
  if (!$this->client->auto_log_stacks) {
    $stack = FALSE;
  }
  elseif (isset($context['backtrace'])) {
    $stack = $context['backtrace'];
  }
  else {

    // Remove any logger stack frames.
    $stack = debug_backtrace($this->client->trace ? 0 : DEBUG_BACKTRACE_IGNORE_ARGS);
    $finder = new ClassFinder();
    if ($stack[0]['file'] === realpath($finder
      ->findFile('Drupal\\Core\\Logger\\LoggerChannel'))) {
      array_shift($stack);
      if ($stack[0]['file'] === realpath($finder
        ->findFile('Psr\\Log\\LoggerTrait'))) {
        array_shift($stack);
      }
    }
  }

  // Allow modules to alter or ignore this message.
  $filter = [
    'level' => $level,
    'message' => $message,
    'context' => $context,
    'data' => &$data,
    'stack' => &$stack,
    'client' => $this->client,
    'process' => !empty($this->config
      ->get('log_levels')[$level + 1]),
  ];
  if (in_array($context['channel'], $this->config
    ->get('ignored_channels') ?: [])) {
    $filter['process'] = FALSE;
  }
  $this->moduleHandler
    ->alter('raven_filter', $filter);
  if (!empty($filter['process'])) {
    $this->client
      ->capture($data, $stack);
  }

  // Record a breadcrumb.
  $breadcrumb = [
    'level' => $level,
    'message' => $message,
    'context' => $context,
    'process' => TRUE,
    'breadcrumb' => [
      'category' => $context['channel'],
      'message' => $formatted_message,
      'level' => $levels[$level],
    ],
  ];
  foreach ([
    '%line',
    '%file',
    '%type',
    '%function',
  ] as $key) {
    if (isset($context[$key])) {
      $breadcrumb['breadcrumb']['data'][substr($key, 1)] = $context[$key];
    }
  }
  $this->moduleHandler
    ->alter('raven_breadcrumb', $breadcrumb);
  if (!empty($breadcrumb['process'])) {
    $this->client->breadcrumbs
      ->record($breadcrumb['breadcrumb']);
  }
}