You are here

function raven_watchdog in Raven: Sentry Integration 7

Same name and namespace in other branches
  1. 7.4 raven.module \raven_watchdog()
  2. 7.2 raven.module \raven_watchdog()
  3. 7.3 raven.module \raven_watchdog()

Implements hook_watchdog().

File

./raven.module, line 253
Allows to track errors to Sentry server.

Code

function raven_watchdog($log_entry) {
  if (!variable_get('raven_enabled', FALSE)) {
    return;
  }
  if (!variable_get('raven_watchdog_handler', FALSE)) {
    return;
  }

  // Do not process php errors.
  if ($log_entry['type'] === 'php') {
    return;
  }
  $watchdog_levels = variable_get('raven_watchdog_levels', array());
  if (empty($watchdog_levels[$log_entry['severity'] + 1])) {
    return;
  }
  $filter = array(
    'process' => TRUE,
    'log_entry' => $log_entry,
  );
  if ($log_entry['type'] === 'page not found') {
    $filter['process'] = (bool) variable_get('raven_watchdog_page_not_found', FALSE);
  }
  drupal_alter('raven_watchdog_filter', $filter);
  if (!$filter['process']) {
    return;
  }
  if (!raven_libraries_load()) {
    return;
  }
  $levels_map = array(
    WATCHDOG_EMERGENCY => Raven_Client::FATAL,
    WATCHDOG_ALERT => Raven_Client::FATAL,
    WATCHDOG_CRITICAL => Raven_Client::FATAL,
    WATCHDOG_ERROR => Raven_Client::ERROR,
    WATCHDOG_WARNING => Raven_Client::WARNING,
    WATCHDOG_NOTICE => Raven_Client::INFO,
    WATCHDOG_INFO => Raven_Client::INFO,
    WATCHDOG_DEBUG => Raven_Client::DEBUG,
  );
  $variables = $log_entry['variables'];
  if (!$variables) {
    $variables = array();
  }
  $data = array(
    'level' => $levels_map[$log_entry['severity']],
    'message' => strip_tags(format_string($log_entry['message'], $variables)),
    'sentry.interfaces.Message' => array(
      'message' => $log_entry['message'],
      'params' => $log_entry['variables'],
    ),
    'tags' => array(
      'type' => $log_entry['type'],
    ),
    'extra' => array(
      'link' => $log_entry['link'],
      'request_uri' => $log_entry['request_uri'],
      'referer' => $log_entry['referer'],
      'ip' => $log_entry['ip'],
    ),
    'logger' => 'watchdog',
  );
  $client = _raven_get_client();
  if (!$client) {
    return;
  }

  // By default, disable reflection tracing for user watchdog entries.
  if ($data['tags']['type'] === 'user' && $client->trace && !variable_get('raven_trace_user', FALSE)) {
    $client->trace = FALSE;
    $client
      ->capture($data, NULL);
    $client->trace = TRUE;
  }
  else {
    $client
      ->capture($data, NULL);
  }
}