watchdog.module 5.71 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':
Dries's avatar
 
Dries committed
23
      return 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.');
Dries's avatar
 
Dries committed
24 25
    case 'admin/logs/error':
      return t('Watchdog events about PHP and database errors.');
Dries's avatar
 
Dries committed
26
    case 'admin/logs/httpd':
Dries's avatar
 
Dries committed
27
      return t('Watchdog events that are from the web server, like 404s, etc.');
Dries's avatar
 
Dries committed
28 29
    case 'admin/logs/regular':
      return t('Watchdog events that are "normal" and have no other classification.');
Dries's avatar
 
Dries committed
30
    case 'admin/logs/search':
Dries's avatar
 
Dries committed
31
      return t('Watchdog events showing what users have searched for.');
Dries's avatar
 
Dries committed
32 33 34 35
    case 'admin/logs/special':
      return t('Watchdog events about adding, changing, and moderating nodes and comments.');
    case 'admin/logs/user':
      return t('Watchdog events that have to do with users and their accounts.');
Dries's avatar
 
Dries committed
36
    case 'admin/logs/warning':
Dries's avatar
 
Dries committed
37
      return t('Watchdog events that don\'t stop normal operation, but are things you should know.');
Dries's avatar
 
Dries committed
38
    case 'admin/modules#description':
Dries's avatar
Dries committed
39
      return t('Logs and records system events.');
Dries's avatar
 
Dries committed
40
  }
41 42
}

Dries's avatar
 
Dries committed
43
/**
Dries's avatar
 
Dries committed
44
 * Implementation of hook_menu().
Dries's avatar
 
Dries committed
45
 */
Dries's avatar
 
Dries committed
46
function watchdog_menu($may_cache) {
Dries's avatar
 
Dries committed
47
  $items = array();
Dries's avatar
 
Dries committed
48

Dries's avatar
 
Dries committed
49 50 51
  if ($may_cache) {
    $items[] = array('path' => 'admin/logs', 'title' => t('logs'),
      'callback' => 'watchdog_overview', 'access' => user_access('administer watchdog'));
Dries's avatar
 
Dries committed
52

Dries's avatar
 
Dries committed
53 54 55
    $items[] = array('path' => 'admin/logs/view', 'title' => t('view details'),
      'callback' => 'watchdog_view', 'access' => user_access('administer watchdog'),
      'type' => MENU_CALLBACK);
Dries's avatar
 
Dries committed
56

Dries's avatar
 
Dries committed
57
    foreach (_watchdog_get_message_types() as $type) {
Dries's avatar
 
Dries committed
58
      $items[] = array('path' => 'admin/logs/'. $type, 'title' => t($type), 'type' => MENU_DYNAMIC_ITEM);
Dries's avatar
 
Dries committed
59
    }
Dries's avatar
 
Dries committed
60
  }
Dries's avatar
 
Dries committed
61
  return $items;
Dries's avatar
 
Dries committed
62 63
}

Dries's avatar
 
Dries committed
64 65 66 67 68 69 70
/**
 * Implementation of hook_perm().
 */
function watchdog_perm() {
  return array('administer watchdog');
}

Dries's avatar
Dries committed
71 72 73 74 75
/**
 * Implementation of hook_cron().
 *
 * Remove expired log messages.
 */
Dries's avatar
 
Dries committed
76
function watchdog_cron() {
Dries's avatar
Dries committed
77
  db_query('DELETE FROM {watchdog} WHERE '. time() .' - timestamp > '. variable_get('watchdog_clear', 604800));
Dries's avatar
 
Dries committed
78
}
Dries's avatar
 
Dries committed
79

Dries's avatar
Dries committed
80 81 82 83
/**
 * Menu callback; displays a listing of log messages.
 */
function watchdog_overview($type = '') {
Dries's avatar
 
Dries committed
84 85 86
  foreach (_watchdog_get_message_types() as $key) {
    $query[$key] = "WHERE type = '". check_query($key) ."'";
  }
Dries's avatar
 
Dries committed
87

Dries's avatar
 
Dries committed
88
  $header = array(
Dries's avatar
 
Dries committed
89 90 91 92
    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
93
  );
94
  $sql = 'SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid '. ($type ? $query[$type] : '') . tablesort_sql($header);
Dries's avatar
 
Dries committed
95
  $result = pager_query($sql, 50);
Dries's avatar
 
Dries committed
96

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

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

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

Dries's avatar
Dries committed
123 124 125
/**
 * Menu callback; displays details about a log message.
 */
Dries's avatar
 
Dries committed
126
function watchdog_view($id) {
Dries's avatar
Dries committed
127 128
  $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
129
  if ($watchdog = db_fetch_object($result)) {
Dries's avatar
Dries committed
130 131 132 133 134 135 136 137
    $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>';
    $output .= ' <tr><th>'. t('Location') ."</th><td>$watchdog->location</td></tr>";
    $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
138
  }
Dries's avatar
Dries committed
139
  print theme('page', $output);
Dries's avatar
 
Dries committed
140
}
Dries's avatar
 
Dries committed
141

Dries's avatar
 
Dries committed
142 143 144
function _watchdog_get_message_types() {
  $types = array();

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

  return $types;
}

Dries's avatar
 
Dries committed
153
?>