You are here

function mongodb_watchdog_overview in MongoDB 6

Same name and namespace in other branches
  1. 7 mongodb_watchdog/mongodb_watchdog.admin.inc \mongodb_watchdog_overview()

Display watchdogs entries in MongoDB.

@TODO Use theme function. Use exposed filter like dblog.

Return value

array a form array

1 string reference to 'mongodb_watchdog_overview'
mongodb_watchdog_menu in mongodb_watchdog/mongodb_watchdog.module
Implements hook_menu().

File

mongodb_watchdog/mongodb_watchdog.admin.inc, line 17
Settings for mongodb. Moved back to module file.

Code

function mongodb_watchdog_overview() {
  drupal_add_css(drupal_get_path('module', 'mongodb_watchdog') . '/mongodb_watchdog.css', 'module', 'all', FALSE);
  $rows = array();
  $icons = array(
    WATCHDOG_DEBUG => '',
    WATCHDOG_INFO => '',
    WATCHDOG_NOTICE => '',
    WATCHDOG_WARNING => theme('image', 'misc/watchdog-warning.png', t('warning'), t('warning')),
    WATCHDOG_ERROR => theme('image', 'misc/watchdog-error.png', t('error'), t('error')),
    WATCHDOG_CRITICAL => theme('image', 'misc/watchdog-error.png', t('critical'), t('critical')),
    WATCHDOG_ALERT => theme('image', 'misc/watchdog-error.png', t('alert'), t('alert')),
    WATCHDOG_EMERG => theme('image', 'misc/watchdog-error.png', t('emergency'), t('emergency')),
  );
  $classes = array(
    WATCHDOG_DEBUG => 'mongodb_watchdog-debug',
    WATCHDOG_INFO => 'mongodb_watchdog-info',
    WATCHDOG_NOTICE => 'mongodb_watchdog-notice',
    WATCHDOG_WARNING => 'mongodb_watchdog-warning',
    WATCHDOG_ERROR => 'mongodb_watchdog-error',
    WATCHDOG_CRITICAL => 'mongodb_watchdog-critical',
    WATCHDOG_ALERT => 'mongodb_watchdog-alert',
    WATCHDOG_EMERG => 'mongodb_watchdog-emergency',
  );
  global $pager_page_array, $pager_total, $pager_total_items, $pager_limits;
  $per_page = 50;
  $page = isset($_GET['page']) ? $_GET['page'] : '';
  $pager_page_array = explode(',', $page);
  $on_page = $pager_page_array[0];
  $cursor = mongodb_collection(variable_get('mongodb_watchdog', 'watchdog'))
    ->find(mongodb_watchdog_build_filter_query())
    ->limit($per_page)
    ->skip($on_page * $per_page)
    ->sort(array(
    'timestamp' => -1,
  ));
  $build['mongodb_watchdog_filter_form'] = mongodb_watchdog_filter_form();
  $build['mongodb_watchdog_clear_log_form'] = mongodb_watchdog_clear_log_form();
  $header = array(
    // Icon column.
    '',
    t('#'),
    array(
      'data' => t('Type'),
    ),
    array(
      'data' => t('Date'),
    ),
    t('Source'),
    t('Message'),
  );
  $rows = array();
  foreach ($cursor as $id => $value) {
    if ($value['type'] == 'php' && $value['message'] == '%type: %message in %function (line %line of %file).') {
      $collection = mongodb_collection(variable_get('mongodb_watchdog', 'watchdog'));
      $collection = $collection->db
        ->selectCollection('watchdog_event_' . $value['_id']);
      if ($result = $collection
        ->find()
        ->sort(array(
        '$natural' => -1,
      ))
        ->limit(1)
        ->getNext()) {
        $value['file'] = basename($result['variables']['%file']);
        $value['line'] = $result['variables']['%line'];
        $value['message'] = '%type in %function';
        $value['variables'] = $result['variables'];
      }
    }
    $message = truncate_utf8(strip_tags(_mongodb_watchdog_format_message($value)), 56, TRUE, TRUE);
    $rows[$id] = array(
      $icons[$value['severity']],
      isset($value['count']) && $value['count'] > 1 ? $value['count'] : '',
      // The entry type comes from code as the first parameter to a watchdog()
      // call and is not a dynamic string, so it is ok to pass to t().
      t($value['type']),
      empty($value['timestamp']) ? '' : format_date($value['timestamp'], 'small'),
      empty($value['file']) ? '' : truncate_utf8(basename($value['file']), 30) . (empty($value['line']) ? '' : '+' . $value['line']),
      l($message, "admin/reports/mongodb/{$id}"),
    );
  }
  $attributes = array(
    'id' => 'admin-mongodb_watchdog',
  );
  $build['mongodb_watchdog_table'] = array(
    '#type' => 'markup',
    '#value' => theme('table', $header, $rows, $attributes),
  );

  // Add the pager.
  if ($on_page > 0 || count($rows) >= $per_page) {
    $pager_total_items[0] = mongodb_collection(variable_get('mongodb_watchdog', 'watchdog'))
      ->find(mongodb_watchdog_build_filter_query())
      ->count();
    $pager_total[0] = ceil($pager_total_items[0] / $per_page);
    $pager_page_array[0] = max(0, min((int) $pager_page_array[0], (int) $pager_total[0] - 1));
    $pager_limits[0] = $per_page;
    $build['pager'] = array(
      '#theme' => 'pager',
    );
  }
  return $build;
}