watchdog.module 6.26 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
      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>
Dries's avatar
 
Dries committed
12
      <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/settings/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/modules#description':
Dries's avatar
Dries committed
30
      return t('Logs and records system events.');
31 32
//    case 'admin/settings/watchdog':
//      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
/**
Dries's avatar
 
Dries committed
44
 * Implementation of hook_menu().
Dries's avatar
 
Dries committed
45
 */
Dries's avatar
 
Dries committed
46 47 48 49 50 51 52 53 54 55 56 57 58
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
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
/**
 * Implementation of hook_cron().
 *
 * Remove expired log messages.
 */
Dries's avatar
 
Dries committed
69
function watchdog_cron() {
Dries's avatar
Dries committed
70
  db_query('DELETE FROM {watchdog} WHERE '. time() .' - timestamp > '. variable_get('watchdog_clear', 604800));
Dries's avatar
 
Dries committed
71
}
Dries's avatar
 
Dries committed
72

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

Dries's avatar
 
Dries committed
82
  $header = array(
Dries's avatar
Dries committed
83 84 85 86
    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
87
  );
Dries's avatar
Dries committed
88
  $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
89 90
  $sql .= tablesort_sql($header);
  $result = pager_query($sql, 50);
Dries's avatar
 
Dries committed
91

Dries's avatar
 
Dries committed
92
  while ($watchdog = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
93
    $rows[] = array(
Dries's avatar
Dries committed
94 95 96 97 98
      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
99
    );
Dries's avatar
 
Dries committed
100
  }
Dries's avatar
 
Dries committed
101

Dries's avatar
 
Dries committed
102
  if (!$rows) {
Dries's avatar
Dries committed
103
    $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => '5'));
Dries's avatar
 
Dries committed
104
  }
Dries's avatar
 
Dries committed
105

Dries's avatar
Dries committed
106
  $pager = theme('pager', NULL, 50, 0, tablesort_pager());
Dries's avatar
 
Dries committed
107
  if (!empty($pager)) {
Dries's avatar
Dries committed
108
    $rows[] = array(array('data' => $pager, 'colspan' => '5'));
Dries's avatar
 
Dries committed
109
  }
Dries's avatar
Dries committed
110
  print theme('page', theme('table', $header, $rows));
Dries's avatar
 
Dries committed
111 112
}

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

Dries's avatar
 
Dries committed
132 133 134
function _watchdog_get_message_types() {
  $types = array();

Dries's avatar
Dries committed
135
  $result = db_query('SELECT DISTINCT(type) FROM {watchdog}');
Dries's avatar
 
Dries committed
136 137 138 139 140 141 142
  while ($object = db_fetch_object($result)) {
    $types[] = $object->type;
  }

  return $types;
}

Dries's avatar
 
Dries committed
143
?>