[ Index ]
 

Code source de Drupal 5.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/modules/watchdog/ -> watchdog.module (source)

   1  <?php
   2  // $Id: watchdog.module,v 1.165.2.1 2007/01/23 19:07:33 dries Exp $
   3  
   4  /**
   5   * @file
   6   * System monitoring and logging for administrators.
   7   *
   8   * The watchdog module monitors your site and keeps a list of
   9   * recorded events containing usage and performance data, errors,
  10   * warnings, and similar operational information.
  11   *
  12   * @see watchdog().
  13   */
  14  
  15  /**
  16   * Implementation of hook_help().
  17   */
  18  function watchdog_help($section) {
  19    switch ($section) {
  20      case 'admin/help#watchdog':
  21        $output = '<p>'. t('The watchdog module monitors your system, capturing system events in a log to be reviewed by an authorized individual at a later time. This is useful for site administrators who want a quick overview of activities on their site. The logs also record the sequence of events, so it can be useful for debugging site errors.') .'</p>';
  22        $output .= '<p>'. t('The watchdog log is simply a list of recorded events containing usage data, performance data, errors, warnings and operational information. Administrators should check the watchdog report on a regular basis to ensure their site is working properly.') .'</p>';
  23        $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@watchdog">Watchdog page</a>.', array('@watchdog' => 'http://drupal.org/handbook/modules/watchdog/')) .'</p>';
  24        return $output;
  25      case 'admin/logs':
  26        return '<p>'. t('The watchdog module monitors your web site, capturing system events in a log to be reviewed by an authorized individual at a later time. The watchdog log is simply a list of recorded events containing usage data, performance data, errors, warnings and operational information. It is vital to check the watchdog report on a regular basis as it is often the only way to tell what is going on.') .'</p>';
  27    }
  28  }
  29  
  30  /**
  31   * Implementation of hook_menu().
  32   */
  33  function watchdog_menu($may_cache) {
  34    $items = array();
  35  
  36    if ($may_cache) {
  37      $items[] = array('path' => 'admin/logs/watchdog', 'title' => t('Recent log entries'),
  38        'description' => t('View events that have recently been logged.'),
  39        'callback' => 'watchdog_overview',
  40        'weight' => -1);
  41      $items[] = array('path' => 'admin/logs/page-not-found', 'title' => t("Top 'page not found' errors"),
  42        'description' => t("View 'page not found' errors (404s)."),
  43        'callback' => 'watchdog_top',
  44        'callback arguments' => array('page not found'));
  45      $items[] = array('path' => 'admin/logs/access-denied', 'title' => t("Top 'access denied' errors"),
  46        'description' => t("View 'access denied' errors (403s)."),
  47        'callback' => 'watchdog_top',
  48        'callback arguments' => array('access denied'));
  49      $items[] = array('path' => 'admin/logs/event', 'title' => t('Details'),
  50        'callback' => 'watchdog_event',
  51        'type' => MENU_CALLBACK);
  52    }
  53    else {
  54      if (arg(0) == 'admin' && arg(1) == 'logs') {
  55        // Add the CSS for this module
  56        drupal_add_css(drupal_get_path('module', 'watchdog') .'/watchdog.css', 'module', 'all', FALSE);
  57      }
  58    }
  59  
  60    return $items;
  61  }
  62  
  63  /**
  64   * Implementation of hook_cron().
  65   *
  66   * Remove expired log messages and flood control events.
  67   */
  68  function watchdog_cron() {
  69    db_query('DELETE FROM {watchdog} WHERE timestamp < %d', time() - variable_get('watchdog_clear', 604800));
  70    db_query('DELETE FROM {flood} WHERE timestamp < %d', time() - 3600);
  71  }
  72  
  73  /**
  74   * Implementation of hook_user().
  75   */
  76  function watchdog_user($op, &$edit, &$user) {
  77    if ($op == 'delete') {
  78      db_query('UPDATE {watchdog} SET uid = 0 WHERE uid = %d', $user->uid);
  79    }
  80  }
  81  
  82  function watchdog_form_overview() {
  83    $names['all'] = t('all messages');
  84    foreach (_watchdog_get_message_types() as $type) {
  85      $names[$type] = t('!type messages', array('!type' => t($type)));
  86    }
  87  
  88    if (empty($_SESSION['watchdog_overview_filter'])) {
  89      $_SESSION['watchdog_overview_filter'] = 'all';
  90    }
  91  
  92    $form['filter'] = array(
  93      '#type' => 'select',
  94      '#title' => t('Filter by message type'),
  95      '#options' => $names,
  96      '#default_value' => $_SESSION['watchdog_overview_filter']
  97    );
  98    $form['submit'] = array('#type' => 'submit', '#value' => t('Filter'));
  99    $form['#redirect'] = FALSE;
 100  
 101    return $form;
 102  }
 103  /**
 104   * Menu callback; displays a listing of log messages.
 105   */
 106  function watchdog_overview() {
 107    $icons = array(WATCHDOG_NOTICE  => '',
 108                   WATCHDOG_WARNING => theme('image', 'misc/watchdog-warning.png', t('warning'), t('warning')),
 109                   WATCHDOG_ERROR   => theme('image', 'misc/watchdog-error.png', t('error'), t('error')));
 110    $classes = array(WATCHDOG_NOTICE => 'watchdog-notice', WATCHDOG_WARNING => 'watchdog-warning', WATCHDOG_ERROR => 'watchdog-error');
 111  
 112    $output = drupal_get_form('watchdog_form_overview');
 113  
 114    $header = array(
 115      ' ',
 116      array('data' => t('Type'), 'field' => 'w.type'),
 117      array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc'),
 118      array('data' => t('Message'), 'field' => 'w.message'),
 119      array('data' => t('User'), 'field' => 'u.name'),
 120      array('data' => t('Operations'))
 121    );
 122  
 123    $sql = "SELECT w.wid, w.uid, w.severity, w.type, w.timestamp, w.message, w.link, u.name FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid";
 124    $tablesort = tablesort_sql($header);
 125    $type = $_SESSION['watchdog_overview_filter'];
 126    if ($type != 'all') {
 127      $result = pager_query($sql ." WHERE w.type = '%s'". $tablesort, 50, 0, NULL, $type);
 128    }
 129    else {
 130      $result = pager_query($sql . $tablesort, 50);
 131    }
 132  
 133    while ($watchdog = db_fetch_object($result)) {
 134      $rows[] = array('data' =>
 135        array(
 136          // Cells
 137          $icons[$watchdog->severity],
 138          t($watchdog->type),
 139          format_date($watchdog->timestamp, 'small'),
 140          l(truncate_utf8($watchdog->message, 56, TRUE, TRUE), 'admin/logs/event/'. $watchdog->wid, array(), NULL, NULL, FALSE, TRUE),
 141          theme('username', $watchdog),
 142          $watchdog->link,
 143        ),
 144        // Attributes for tr
 145        'class' => "watchdog-". preg_replace('/[^a-z]/i', '-', $watchdog->type) .' '. $classes[$watchdog->severity]
 146      );
 147    }
 148  
 149    if (!$rows) {
 150      $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => 6));
 151    }
 152  
 153    $output .= theme('table', $header, $rows);
 154    $output .= theme('pager', NULL, 50, 0);
 155  
 156    return $output;
 157  }
 158  
 159  /**
 160   * Menu callback; generic function to display a page of the most frequent
 161   * watchdog events of a specified type.
 162   */
 163  function watchdog_top($type) {
 164  
 165    $header = array(
 166      array('data' => t('Count'), 'field' => 'count', 'sort' => 'desc'),
 167      array('data' => t('Message'), 'field' => 'message')
 168    );
 169  
 170    $result = pager_query("SELECT COUNT(wid) AS count, message FROM {watchdog} WHERE type = '%s' GROUP BY message ". tablesort_sql($header), 30, 0, "SELECT COUNT(DISTINCT(message)) FROM {watchdog} WHERE type = '%s'", $type);
 171  
 172    while ($watchdog = db_fetch_object($result)) {
 173      $rows[] = array($watchdog->count, truncate_utf8($watchdog->message, 56, TRUE, TRUE));
 174    }
 175  
 176    if (!$rows) {
 177      $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => 2));
 178    }
 179  
 180    $output  = theme('table', $header, $rows);
 181    $output .= theme('pager', NULL, 30, 0);
 182  
 183    return $output;
 184  }
 185  
 186  function theme_watchdog_form_overview($form) {
 187    return '<div class="container-inline">'. drupal_render($form) .'</div>';
 188  }
 189  
 190  function watchdog_form_overview_submit($form_id, $form_values) {
 191    $_SESSION['watchdog_overview_filter'] = $form_values['filter'];
 192  }
 193  
 194  /**
 195   * Menu callback; displays details about a log message.
 196   */
 197  function watchdog_event($id) {
 198    $severity = array(WATCHDOG_NOTICE => t('notice'), WATCHDOG_WARNING => t('warning'), WATCHDOG_ERROR => t('error'));
 199    $output = '';
 200    $result = db_query('SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid WHERE w.wid = %d', $id);
 201    if ($watchdog = db_fetch_object($result)) {
 202      $rows = array(
 203        array(
 204          array('data' => t('Type'), 'header' => TRUE),
 205          t($watchdog->type),
 206        ),
 207        array(
 208          array('data' => t('Date'), 'header' => TRUE),
 209          format_date($watchdog->timestamp, 'large'),
 210        ),
 211        array(
 212          array('data' => t('User'), 'header' => TRUE),
 213          theme('username', $watchdog),
 214        ),
 215        array(
 216          array('data' => t('Location'), 'header' => TRUE),
 217          l($watchdog->location, $watchdog->location),
 218        ),
 219        array(
 220          array('data' => t('Referrer'), 'header' => TRUE),
 221          l($watchdog->referer, $watchdog->referer),
 222        ),
 223        array(
 224          array('data' => t('Message'), 'header' => TRUE),
 225          $watchdog->message,
 226        ),
 227        array(
 228          array('data' => t('Severity'), 'header' => TRUE),
 229          $severity[$watchdog->severity],
 230        ),
 231        array(
 232          array('data' => t('Hostname'), 'header' => TRUE),
 233          $watchdog->hostname,
 234        ),
 235      );
 236      $attributes = array('class' => 'watchdog-event');
 237      $output = theme('table', array(), $rows, $attributes);
 238    }
 239    return $output;
 240  }
 241  
 242  function _watchdog_get_message_types() {
 243    $types = array();
 244  
 245    $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
 246    while ($object = db_fetch_object($result)) {
 247      $types[] = $object->type;
 248    }
 249  
 250    return $types;
 251  }


Généré le : Fri Nov 30 16:20:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics