watchdog.module 5.62 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
2
// $Id$
Dries's avatar
 
Dries committed
3

Dries's avatar
 
Dries committed
4 5 6 7 8 9 10 11 12 13 14
/**
 * @file
 * System monitoring and logging for administrators.
 *
 * The watchdog module monitors your site and keeps a list of
 * recorded events containing usage and performance data, errors,
 * warnings, and similar operational information.
 *
 * @see watchdog().
 */

Dries's avatar
Dries committed
15 16 17 18
/**
 * Implementation of hook_help().
 */
function watchdog_help($section = 'admin/help#watchdog') {
Dries's avatar
 
Dries committed
19
  switch ($section) {
Dries's avatar
 
Dries committed
20
    case 'admin/help#watchdog':
Dries's avatar
 
Dries committed
21
      return t('<p>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 <a href="%watchdog">check the watchdog report</a> on a regular basis as it is often the only way to tell what is going on.</p>', array('%watchdog' => url('admin/logs')));
Dries's avatar
 
Dries committed
22
    case 'admin/logs':
23
      return t('<p>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>');
Dries's avatar
 
Dries committed
24
    case 'admin/modules#description':
Dries's avatar
Dries committed
25
      return t('Logs and records system events.');
Dries's avatar
 
Dries committed
26
  }
27 28
}

Dries's avatar
 
Dries committed
29
/**
Dries's avatar
 
Dries committed
30
 * Implementation of hook_menu().
Dries's avatar
 
Dries committed
31
 */
Dries's avatar
 
Dries committed
32
function watchdog_menu($may_cache) {
Dries's avatar
 
Dries committed
33
  $items = array();
Dries's avatar
 
Dries committed
34

Dries's avatar
 
Dries committed
35 36 37
  if ($may_cache) {
    $items[] = array('path' => 'admin/logs', 'title' => t('logs'),
      'callback' => 'watchdog_overview', 'access' => user_access('administer watchdog'));
38 39
    $items[] = array('path' => 'admin/logs/event', 'title' => t('details'),
      'callback' => 'watchdog_event', 'access' => user_access('administer watchdog'),
Dries's avatar
 
Dries committed
40
      'type' => MENU_CALLBACK);
Dries's avatar
 
Dries committed
41
  }
Dries's avatar
 
Dries committed
42
  return $items;
Dries's avatar
 
Dries committed
43 44
}

Dries's avatar
 
Dries committed
45 46 47 48 49 50 51
/**
 * Implementation of hook_perm().
 */
function watchdog_perm() {
  return array('administer watchdog');
}

Dries's avatar
Dries committed
52 53 54
/**
 * Implementation of hook_cron().
 *
Dries's avatar
 
Dries committed
55
 * Remove expired log messages and flood control events.
Dries's avatar
Dries committed
56
 */
Dries's avatar
 
Dries committed
57
function watchdog_cron() {
Dries's avatar
 
Dries committed
58
  db_query('DELETE FROM {watchdog} WHERE timestamp < %d', time() - variable_get('watchdog_clear', 604800));
Dries's avatar
 
Dries committed
59
  db_query('DELETE FROM {flood} WHERE timestamp < %d', time() - 3600);
Dries's avatar
 
Dries committed
60
}
Dries's avatar
 
Dries committed
61

Dries's avatar
Dries committed
62 63 64
/**
 * Menu callback; displays a listing of log messages.
 */
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
function watchdog_overview() {
  $names['all'] = t('all messages');
  $queries['all'] = '';
  foreach (_watchdog_get_message_types() as $type) {
    $names[$type] = t('%type messages', array('%type' => $type));
    $queries[$type] = "WHERE type = '". db_escape_string($type) ."'";
  }

  if (empty($_SESSION['watchdog_overview_filter'])) {
    $_SESSION['watchdog_overview_filter'] = 'all';
  }

  $op = $_POST['op'];
  if ($op == t('Filter') && isset($_POST['edit']['filter'])) {
    $_SESSION['watchdog_overview_filter'] = $_POST['edit']['filter'];
Dries's avatar
 
Dries committed
80
  }
Dries's avatar
 
Dries committed
81

Dries's avatar
 
Dries committed
82
  $form  = form_select(t('Filter by message type'), 'filter', $_SESSION['watchdog_overview_filter'], $names);
83 84
  $form .= form_submit(t('Filter'));

Dries's avatar
 
Dries committed
85
  $header = array(
86
    array('data' => t('Type'), 'field' => 'w.type'),
Dries's avatar
 
Dries committed
87 88 89 90
    array('data' => t('Date'), 'field' => 'w.timestamp', 'sort' => 'desc'),
    array('data' => t('Message'), 'field' => 'w.message'),
    array('data' => t('User'), 'field' => 'u.name'),
    array('data' => t('Operations'), 'colspan' => '2')
Dries's avatar
 
Dries committed
91
  );
92
  $sql = 'SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid '. $queries[$_SESSION['watchdog_overview_filter']] . tablesort_sql($header);
Dries's avatar
 
Dries committed
93
  $result = pager_query($sql, 50);
Dries's avatar
 
Dries committed
94

Dries's avatar
 
Dries committed
95
  while ($watchdog = db_fetch_object($result)) {
96 97 98
    $rows[] = array('data' =>
      array(
        // Cells
99
        $watchdog->type,
100 101 102 103
        format_date($watchdog->timestamp, 'small'),
        truncate_utf8(strip_tags($watchdog->message), 64),
        format_name($watchdog),
        $watchdog->link,
104
        l(t('details'), "admin/logs/event/$watchdog->wid")
105 106
      ),
      // Attributes for tr
Dries's avatar
 
Dries committed
107
      'class' => "watchdog-$watchdog->type"
Dries's avatar
 
Dries committed
108
    );
Dries's avatar
 
Dries committed
109
  }
Dries's avatar
 
Dries committed
110

Dries's avatar
 
Dries committed
111
  if (!$rows) {
Dries's avatar
 
Dries committed
112
    $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => '6'));
Dries's avatar
 
Dries committed
113
  }
Dries's avatar
 
Dries committed
114

Dries's avatar
Dries committed
115
  $pager = theme('pager', NULL, 50, 0, tablesort_pager());
Dries's avatar
 
Dries committed
116
  if (!empty($pager)) {
Dries's avatar
 
Dries committed
117
    $rows[] = array(array('data' => $pager, 'colspan' => '6'));
Dries's avatar
 
Dries committed
118
  }
119 120 121 122 123

  $output  = '<div class="container-inline">'. form($form) .'</div>';
  $output .= theme('table', $header, $rows);

  print theme('page', $output);
Dries's avatar
 
Dries committed
124 125
}

Dries's avatar
Dries committed
126 127 128
/**
 * Menu callback; displays details about a log message.
 */
129
function watchdog_event($id) {
Dries's avatar
Dries committed
130 131
  $output = '';
  $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);
Dries's avatar
 
Dries committed
132
  if ($watchdog = db_fetch_object($result)) {
Dries's avatar
Dries committed
133 134 135 136
    $output .= '<table border="1" cellpadding="2" cellspacing="2">';
    $output .= ' <tr><th>'. t('Type') ."</th><td>$watchdog->type</td></tr>";
    $output .= ' <tr><th>'. t('Date') .'</th><td>'. format_date($watchdog->timestamp, 'large') .'</td></tr>';
    $output .= ' <tr><th>'. t('User') .'</th><td>'. format_name($watchdog) .'</td></tr>';
137
    $output .= ' <tr><th>'. t('Location') ."</th><td>". l($watchdog->location, $watchdog->location) ."</td></tr>";
Dries's avatar
Dries committed
138 139 140
    $output .= ' <tr><th>'. t('Message') ."</th><td>$watchdog->message</td></tr>";
    $output .= ' <tr><th>'. t('Hostname') ."</th><td>$watchdog->hostname</td></tr>";
    $output .= '</table>';
Dries's avatar
 
Dries committed
141
  }
Dries's avatar
Dries committed
142
  print theme('page', $output);
Dries's avatar
 
Dries committed
143
}
Dries's avatar
 
Dries committed
144

Dries's avatar
 
Dries committed
145 146 147
function _watchdog_get_message_types() {
  $types = array();

Dries's avatar
Dries committed
148
  $result = db_query('SELECT DISTINCT(type) FROM {watchdog}');
Dries's avatar
 
Dries committed
149 150 151 152 153 154 155
  while ($object = db_fetch_object($result)) {
    $types[] = $object->type;
  }

  return $types;
}

Dries's avatar
 
Dries committed
156
?>