watchdog.module 6.67 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
/**
 * Implementation of hook_help().
 */
function watchdog_help($section = 'admin/help#watchdog') {
Dries's avatar
 
Dries committed
8
  switch ($section) {
Dries's avatar
 
Dries committed
9
    case 'admin/help#watchdog':
Dries's avatar
Dries committed
10 11 12
      return t('
      <p>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>
      <p>To ease administration, the watchdog will automatically discard old log entries, <a href="%log-entry">as configured</a>. Needs "cron.php" to discard the entries.</p>', array('%watchdog' => url('admin/logs'), '%log-entry' => url('admin/system/modules/watchdog')));
Dries's avatar
 
Dries committed
13
    case 'admin/logs':
Dries's avatar
Dries committed
14
      return t('The watchdog module monitors your web site, captures system events in a log and records them to be reviewed by an authorized individual at a later time.  The watchdog log is simply a list of events recorded during operation and contains 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
15
    case 'admin/logs/user':
Dries's avatar
Dries committed
16
      return t('Watchdog events that have to do with users.');
Dries's avatar
 
Dries committed
17
    case 'admin/logs/regular':
Dries's avatar
Dries committed
18
      return t('Watchdog events that are "normal" and have no other classification.');
Dries's avatar
 
Dries committed
19
    case 'admin/logs/httpd':
Dries's avatar
Dries committed
20
      return t('Watchdog events that are from the web server.');
Dries's avatar
 
Dries committed
21
    case 'admin/logs/special':
Dries's avatar
Dries committed
22
      return t('Watchdog events about adding, changing, and moderating nodes and comments.');
Dries's avatar
 
Dries committed
23
    case 'admin/logs/search':
Dries's avatar
Dries committed
24
      return t('Watchdog events showing what users searched for.');
Dries's avatar
 
Dries committed
25
    case 'admin/logs/error':
Dries's avatar
Dries committed
26
      return t('Watchdog events about PHP and database errors.');
Dries's avatar
 
Dries committed
27
    case 'admin/logs/warning':
Dries's avatar
Dries committed
28
      return t('Watchdog warning events. These events don\'t stop Drupal from running, but are things you should know.');
Dries's avatar
 
Dries committed
29
    case 'admin/system/modules#description':
Dries's avatar
Dries committed
30
      return t('Logs and records system events.');
Dries's avatar
 
Dries committed
31
    case 'admin/system/modules/watchdog':
Dries's avatar
Dries committed
32
      return t('Watchdog logs your system events.  To see these events go to the <a href="%watchdog">logs</a>. Since these logs can grow out of control if kept around forever, below set how long an item should be kept in the log.  Note that to discard entries as set below you must run "cron.php" regularly.', array('%watchdog' => url('admin/logs')));
Dries's avatar
 
Dries committed
33
  }
34 35
}

Dries's avatar
Dries committed
36 37 38
/**
 * Implementation of hook_perm().
 */
Dries's avatar
 
Dries committed
39
function watchdog_perm() {
Dries's avatar
Dries committed
40
  return array('administer watchdog');
Dries's avatar
 
Dries committed
41 42
}

Dries's avatar
 
Dries committed
43 44 45
/**
 * Implementation of hook_link().
 */
Dries's avatar
 
Dries committed
46
function watchdog_link($type) {
Dries's avatar
 
Dries committed
47
  if ($type == 'system') {
Dries's avatar
Dries committed
48 49
    menu('admin/logs', t('logs'), user_access('administer watchdog') ? 'watchdog_overview' : MENU_DENIED, 7);
    menu('admin/logs/view', t('view details'), user_access('administer watchdog') ? 'watchdog_view' : MENU_DENIED, 0, MENU_HIDE, MENU_LOCKED);
Dries's avatar
 
Dries committed
50

Kjartan's avatar
Kjartan committed
51 52
    if (arg(1) == 'logs') {
      foreach (_watchdog_get_message_types() as $type) {
53
        menu("admin/logs/$type", t($type), MENU_FALLTHROUGH, 0, MENU_SHOW, MENU_LOCKED);
Kjartan's avatar
Kjartan committed
54
      }
Dries's avatar
 
Dries committed
55
    }
Dries's avatar
 
Dries committed
56 57 58
  }
}

Dries's avatar
Dries committed
59 60 61
/**
 * Implementation of hook_settings().
 */
62
function watchdog_settings() {
Dries's avatar
Dries committed
63
  $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200), 'format_interval');
Dries's avatar
 
Dries committed
64
  $period[1000000000] = t('Never');
Dries's avatar
Dries committed
65
  $output .= form_select(t('Discard entries older than'), 'watchdog_clear', variable_get('watchdog_clear', 604800), $period, t('The time watchdog entries should be kept.  Older entries will be automatically discarded.  Requires crontab.'));
Dries's avatar
 
Dries committed
66 67 68
  return $output;
}

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

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

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

Dries's avatar
 
Dries committed
97
  while ($watchdog = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
98
    $rows[] = array(
Dries's avatar
Dries committed
99 100 101 102 103
      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
104
    );
Dries's avatar
 
Dries committed
105
  }
Dries's avatar
 
Dries committed
106

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

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

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

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

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

  return $types;
}

Dries's avatar
 
Dries committed
148
?>