You are here

function mongodb_watchdog_watchdog in MongoDB 6

Same name and namespace in other branches
  1. 8 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.

http://drupal.org/node/1355808

File

mongodb_watchdog/mongodb_watchdog.module, line 77

Code

function mongodb_watchdog_watchdog(array $log_entry) {
  $watchdog_limit = variable_get('watchdog_limit', WATCHDOG_DEBUG);
  if (isset($log_entry['severity']) && $log_entry['severity'] > $watchdog_limit) {
    return;
  }
  static $checked_ids = array();

  // Find the function that generated this error.
  $log_entry = (array) $log_entry;
  _mongodb_watchdog_enhance_log_entry($log_entry, debug_backtrace());
  $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' => isset($account->name) ? $account->name : variable_get('anonymous', t('Anonymous')),
      'uid' => $log_entry['user']->uid,
    ),
    'ip' => $log_entry['ip'],
    'request_uri' => $log_entry['request_uri'],
    'referer' => $log_entry['referer'],
    'link' => $log_entry['link'],
  );
  unset($log_entry['variables'], $log_entry['user'], $log_entry['ip'], $log_entry['request_uri'], $log_entry['referer'], $log_entry['link']);
  $newobj = array(
    '$set' => $log_entry,
    '$inc' => array(
      'count' => 1,
    ),
  );
  $collection = mongodb_collection(variable_get('mongodb_watchdog', 'watchdog'));
  $id = md5($log_entry['function'] . ':' . $log_entry['line'] . ':' . $log_entry['severity'] . ':' . $log_entry['type'] . ':' . $log_entry['message']);
  if (!isset($checked_ids[$id])) {
    $checked_ids[$id] = $collection
      ->findOne(array(
      '_id' => $id,
    ), array(
      '_id' => 1,
    ));
  }
  $collection
    ->update(array(
    '_id' => $id,
  ), $newobj, array(
    'upsert' => TRUE,
  ) + mongodb_default_write_options(TRUE));
  $collection = $collection->db
    ->selectCollection('watchdog_event_' . $id);
  if (empty($checked_ids[$id])) {
    $max = variable_get('mongodb_watchdog_items', 10000);
    $command = array(
      'create' => $collection
        ->getName(),
      'capped' => TRUE,
      'size' => $max * 1000,
      "max" => $max,
    );
    $collection->db
      ->command($command);
    $checked_ids[$id] = TRUE;
  }
  $collection
    ->insert($event, mongodb_default_write_options(FALSE));
}