watchdog.module 5.89 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 15
/**
 * @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
16 17 18 19
/**
 * Implementation of hook_help().
 */
function watchdog_help($section = 'admin/help#watchdog') {
Dries's avatar
 
Dries committed
20
  switch ($section) {
Dries's avatar
 
Dries committed
21
    case 'admin/help#watchdog':
Dries's avatar
 
Dries committed
22
      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
23
    case 'admin/logs':
Dries's avatar
 
Dries committed
24
      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
25
    case 'admin/logs/user':
Dries's avatar
 
Dries committed
26
      return t('Watchdog events that have to do with users and their accounts.');
Dries's avatar
 
Dries committed
27
    case 'admin/logs/regular':
Dries's avatar
Dries committed
28
      return t('Watchdog events that are "normal" and have no other classification.');
Dries's avatar
 
Dries committed
29
    case 'admin/logs/httpd':
Dries's avatar
 
Dries committed
30
      return t('Watchdog events that are from the web server, like 404s, etc.');
Dries's avatar
 
Dries committed
31
    case 'admin/logs/special':
Dries's avatar
Dries committed
32
      return t('Watchdog events about adding, changing, and moderating nodes and comments.');
Dries's avatar
 
Dries committed
33
    case 'admin/logs/search':
Dries's avatar
 
Dries committed
34
      return t('Watchdog events showing what users have searched for.');
Dries's avatar
 
Dries committed
35
    case 'admin/logs/error':
Dries's avatar
Dries committed
36
      return t('Watchdog events about PHP and database errors.');
Dries's avatar
 
Dries committed
37
    case 'admin/logs/warning':
Dries's avatar
Dries committed
38
      return t('Watchdog warning events. These events don\'t stop Drupal from running, but are things you should know.');
Dries's avatar
 
Dries committed
39
    case 'admin/modules#description':
Dries's avatar
Dries committed
40
      return t('Logs and records system events.');
Dries's avatar
 
Dries committed
41
  }
42 43
}

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

Dries's avatar
 
Dries committed
51
/**
Dries's avatar
 
Dries committed
52
 * Implementation of hook_menu().
Dries's avatar
 
Dries committed
53
 */
Dries's avatar
 
Dries committed
54 55 56 57 58 59 60 61 62 63 64 65 66
function watchdog_menu() {
  $items = array();
  $items[] = array('path' => 'admin/logs', 'title' => t('logs'),
    'callback' => 'watchdog_overview',
    'access' => user_access('administer watchdog'));
  $items[] = array('path' => 'admin/logs/view', 'title' => t('view details'),
    'callback' => 'watchdog_view',
    'access' => user_access('administer watchdog'),
    'type' => MENU_CALLBACK);
  if (arg(1) == 'logs') {
    foreach (_watchdog_get_message_types() as $type) {
      $items[] = array('path' => 'admin/logs/'. $type, 'title' => t($type),
        'type' => MENU_DYNAMIC_ITEM);
Dries's avatar
 
Dries committed
67
    }
Dries's avatar
 
Dries committed
68
  }
Dries's avatar
 
Dries committed
69
  return $items;
Dries's avatar
 
Dries committed
70 71
}

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

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

Dries's avatar
 
Dries committed
89
  $header = array(
Dries's avatar
Dries committed
90 91 92 93
    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
94
  );
95
  $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
96
  $result = pager_query($sql, 50);
Dries's avatar
 
Dries committed
97

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

Dries's avatar
 
Dries committed
108
  if (!$rows) {
Dries's avatar
Dries committed
109
    $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => '5'));
Dries's avatar
 
Dries committed
110
  }
Dries's avatar
 
Dries committed
111

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

Dries's avatar
Dries committed
119 120 121
/**
 * Menu callback; displays details about a log message.
 */
Dries's avatar
 
Dries committed
122
function watchdog_view($id) {
Dries's avatar
Dries committed
123 124
  $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
125
  if ($watchdog = db_fetch_object($result)) {
Dries's avatar
Dries committed
126 127 128 129 130 131 132 133
    $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
134
  }
Dries's avatar
Dries committed
135
  print theme('page', $output);
Dries's avatar
 
Dries committed
136
}
Dries's avatar
 
Dries committed
137

Dries's avatar
 
Dries committed
138 139 140
function _watchdog_get_message_types() {
  $types = array();

Dries's avatar
Dries committed
141
  $result = db_query('SELECT DISTINCT(type) FROM {watchdog}');
Dries's avatar
 
Dries committed
142 143 144 145 146 147 148
  while ($object = db_fetch_object($result)) {
    $types[] = $object->type;
  }

  return $types;
}

Dries's avatar
 
Dries committed
149
?>