You are here

public function LoggerController::watchdogEvent in MongoDB 8

Display watchdogs entry details in MongoDB.

Parameters

string $template_id:

File

src/Logger/LoggerController.php, line 205
Controller service for the MongoDB Watchdog reports.

Class

LoggerController

Namespace

Drupal\mongodb\Logger

Code

public function watchdogEvent($template_id) {
  $event_template = $this->logger
    ->eventLoad($template_id);
  if (empty($event_template)) {
    drupal_set_message($message = t('Template not found, faking it for debug'), 'warning');
    watchdog(__CLASS__, $message, array(), WATCHDOG_DEBUG);
    $event_template = array(
      '_id' => str_repeat('0', 32),
      'type' => 'fake template',
      'severity' => WATCHDOG_DEBUG,
      'function' => '(function unknown)',
      'file' => '(file unknown)',
      'line' => '(line unknown)',
      'count' => 0,
    );
  }
  $severity = watchdog_severity_levels();
  $rows = array(
    array(
      array(
        'data' => t('Type'),
        'header' => TRUE,
      ),
      t($event_template['type']),
    ),
    array(
      array(
        'data' => t('Severity'),
        'header' => TRUE,
      ),
      $severity[$event_template['severity']],
    ),
    array(
      array(
        'data' => t('Function'),
        'header' => TRUE,
      ),
      isset($event_template['function']) ? $event_template['function'] : '',
    ),
    array(
      array(
        'data' => t('File'),
        'header' => TRUE,
      ),
      isset($event_template['file']) ? $event_template['file'] : '',
    ),
    array(
      array(
        'data' => t('Line'),
        'header' => TRUE,
      ),
      isset($event_template['line']) ? $event_template['line'] : '',
    ),
    array(
      array(
        'data' => t('Count'),
        'header' => TRUE,
      ),
      isset($event_template['count']) ? $event_template['count'] : '',
    ),
  );
  $build['reports'] = array(
    '#type' => 'markup',
    '#markup' => l(t('Return to log report'), 'admin/reports/mongodb'),
  );
  $build['mongodb_watchdog_event_table']['header'] = array(
    '#theme' => 'table',
    '#rows' => $rows,
    '#attributes' => array(
      'class' => array(
        'dblog-event',
      ),
    ),
  );

  // @todo: the count is unreliable, so just get the actual number of entries.

  //$total = min($dblog['count'], variable_get('mongodb_watchdog_items', 10000));
  $collection = $this->logger
    ->eventCollection($event_template['_id']);
  $total = $collection
    ->count();
  $limit = 20;
  $page_number = $this
    ->pagerInit(0, $limit, $total);
  $result = $collection
    ->find()
    ->skip($page_number * $limit)
    ->limit($limit)
    ->sort(array(
    '$natural' => -1,
  ));
  $rows = array();
  $header = array(
    array(
      'data' => t('Date'),
      'header' => TRUE,
    ),
    array(
      'data' => t('User'),
      'header' => TRUE,
    ),
    array(
      'data' => t('Location'),
      'header' => TRUE,
    ),
    array(
      'data' => t('Referrer'),
      'header' => TRUE,
    ),
    array(
      'data' => t('Hostname'),
      'header' => TRUE,
    ),
    array(
      'data' => t('Message'),
      'header' => TRUE,
    ),
    array(
      'data' => t('Operations'),
      'header' => TRUE,
    ),
  );
  foreach ($result as $event) {
    if (isset($event['wd-user'])) {
      $account = $event['wd-user'];
      unset($event['wd-user']);
      $ip = $event_template['ip'];
      $request_uri = $event_template['request_uri'];
      $referer = $event_template['referer'];
      $link = $event_template['link'];
      $event_template['variables'] = $event;
    }
    else {
      $account = $event['user'];
      $ip = $event['ip'];
      $request_uri = $event['request_uri'];
      $referer = $event['referer'];
      $link = $event['link'];
      $event_template['variables'] = $event['variables'];
    }
    $rows[] = array(
      format_date($event['timestamp'], 'short'),
      l($account['name'], 'user/' . $account['uid']),
      $request_uri ? l(truncate_utf8(basename($request_uri), 20), $request_uri) : '',
      $referer ? l(truncate_utf8(basename($referer), 20), $referer) : '',
      String::checkPlain($ip),
      $this
        ->formatMessage($event_template),
      $link,
    );
  }
  $build['mongodb_watchdog_event_table']['messages'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
  );
  if ($total > $limit) {
    $build['mongodb_watchdog_event_table']['pager'] = array(
      '#theme' => 'pager',
    );
  }
  return $build;
}