You are here

function mongodb_watchdog_watchdog in MongoDB 8

Same name and namespace in other branches
  1. 6 mongodb_watchdog/mongodb_watchdog.module \mongodb_watchdog_watchdog()
  2. 7 mongodb_watchdog/mongodb_watchdog.module \mongodb_watchdog_watchdog()

Implements hook_watchdog().

Refer to issue #1355808 regarding filtering.

@link http://drupal.org/node/1355808

File

mongodb_watchdog/mongodb_watchdog.module, line 24
Fires watchdog messages to mongodb.

Code

function mongodb_watchdog_watchdog(array $log_entry) {
  $watchdog_limit = Settings::get('watchdog_limit', WATCHDOG_DEBUG);
  if (isset($log_entry['severity']) && $log_entry['severity'] > $watchdog_limit) {
    return;
  }

  // Find the function that generated this error.
  $log_entry = (array) $log_entry;
  _mongodb_watchdog_enhance_log_entry($log_entry, debug_backtrace());

  /** @var \Drupal\Core\Session\UserSession $account */
  $account = $log_entry['user'];

  // Special handling for core bug #904994:
  if (!isset($log_entry['variables'])) {
    $special_messages = array(
      'page not found' => 'Page not found: @param',
      'access denied' => 'Access denied: @param',
    );
    $type = $log_entry['type'];
    $log_entry['variables'] = array(
      '@param' => $log_entry['message'],
    );
    $log_entry['message'] = isset($special_messages[$type]) ? $special_messages[$log_entry['type']] : '@param';
  }
  $event = array(
    'variables' => $log_entry['variables'],
    'timestamp' => $log_entry['timestamp'],
    'user' => array(
      'name' => $account
        ->getUsername($account->name),
      'uid' => $log_entry['uid'],
    ),
    'ip' => $log_entry['ip'],
    'request_uri' => $log_entry['request_uri'],
    'referer' => $log_entry['referer'],
    'link' => $log_entry['link'],
  );
  unset($log_entry['ip'], $log_entry['link'], $log_entry['referer'], $log_entry['request_uri'], $log_entry['user'], $log_entry['variables']);

  /** @var \Drupal\mongodb\Logger\Logger $mongo */
  $logger = Drupal::service('mongo.logger');
  $collection = $logger
    ->templatesCollection();
  $id = md5($log_entry['function'] . ':' . $log_entry['line'] . ':' . $log_entry['severity'] . ':' . $log_entry['type'] . ':' . $log_entry['message']);
  $criteria = array(
    '_id' => $id,
  );
  $update = array(
    '$set' => $log_entry,
    '$inc' => array(
      'count' => 1,
    ),
  );
  $options = array(
    'upsert' => TRUE,
  );
  $collection
    ->update($criteria, $update, $options);
  $result = $collection->db
    ->command(array(
    'getlasterror' => 1,
  ));
  $collection = $collection->db
    ->selectCollection('watchdog_event_' . $id);
  if (empty($result['updatedExisting'])) {
    $max = Settings::get('mongodb_watchdog_items', 10000);
    $command = array(
      'create' => $collection
        ->getName(),
      'capped' => TRUE,
      'size' => $max * 1000,
      "max" => $max,
    );
    $collection->db
      ->command($command);
  }
  $collection
    ->insert($event);
}