watchdog.module 6.15 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/logs':
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 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
22
    case 'admin/modules#description':
Dries's avatar
Dries committed
23
      return t('Logs and records system events.');
Dries's avatar
   
Dries committed
24
  }
25
26
}

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

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

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

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

Dries's avatar
Dries committed
60
61
62
/**
 * Menu callback; displays a listing of log messages.
 */
63
function watchdog_overview() {
64
65
66
67
68
  $icons = array(WATCHDOG_NOTICE  => '',
                 WATCHDOG_WARNING => theme('image', 'misc/watchdog-warning.png', t('warning'), t('warning')),
                 WATCHDOG_ERROR   => theme('image', 'misc/watchdog-error.png', t('error'), t('error')));
  $classes = array(WATCHDOG_NOTICE => 'watchdog-notice', WATCHDOG_WARNING => 'watchdog-warning', WATCHDOG_ERROR => 'watchdog-error');

69
70
71
  $names['all'] = t('all messages');
  $queries['all'] = '';
  foreach (_watchdog_get_message_types() as $type) {
Dries's avatar
   
Dries committed
72
    $names[$type] = t('%type messages', array('%type' => t($type)));
73
74
75
76
77
78
79
    $queries[$type] = "WHERE type = '". db_escape_string($type) ."'";
  }

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

80
81
  if (empty($_SESSION['watchdog_overview_filter'])) {
    $_SESSION['watchdog_overview_filter'] = 'all';
Dries's avatar
   
Dries committed
82
  }
Dries's avatar
 
Dries committed
83

84
85
86
87
88
89
90
91
  $form['filter'] = array(
    type => 'select',
    title => t('Filter by message type'),
    options => $names,
    default_value => $_SESSION['watchdog_overview_filter']
  );

  $form['submit'] = array(type => 'submit', value =>t('Filter'));
92
  $output = drupal_get_form('watchdog_form_overview', $form);
93

Dries's avatar
   
Dries committed
94
  $header = array(
95
    ' ',
96
    array('data' => t('Type'), 'field' => 'w.type'),
97
    array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc'),
Dries's avatar
   
Dries committed
98
99
100
    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
101
  );
102
  $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
103
  $result = pager_query($sql, 50);
Dries's avatar
   
Dries committed
104

Dries's avatar
 
Dries committed
105
  while ($watchdog = db_fetch_object($result)) {
106
107
108
    $rows[] = array('data' =>
      array(
        // Cells
109
        $icons[$watchdog->severity],
Dries's avatar
   
Dries committed
110
        t($watchdog->type),
111
        format_date($watchdog->timestamp, 'small'),
112
        truncate_utf8($watchdog->message, 64),
113
        theme('username', $watchdog),
114
        $watchdog->link,
115
        l(t('details'), "admin/logs/event/$watchdog->wid")
116
117
      ),
      // Attributes for tr
118
      'class' => "watchdog-". preg_replace('/[^a-z]/i', '-', $watchdog->type) .' '. $classes[$watchdog->severity]
Dries's avatar
   
Dries committed
119
    );
Dries's avatar
 
Dries committed
120
  }
Dries's avatar
   
Dries committed
121

Dries's avatar
   
Dries committed
122
  if (!$rows) {
123
    $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => '7'));
Dries's avatar
   
Dries committed
124
  }
Dries's avatar
   
Dries committed
125

126

127
  $output .= theme('table', $header, $rows);
128
  $output .= theme('pager', NULL, 50, 0, tablesort_pager());
129

Dries's avatar
   
Dries committed
130
  return $output;
Dries's avatar
 
Dries committed
131
132
}

133
134
135
136
137
138
139
140
141
function theme_watchdog_form_overview($form) {
  return '<div class="container-inline">'. form_render($form) .'</div>';
}

function watchdog_form_overview_execute($form_id, $form) {
  global $form_values;
  $_SESSION['watchdog_overview_filter'] = $form_values['filter'];
}

Dries's avatar
Dries committed
142
143
144
/**
 * Menu callback; displays details about a log message.
 */
145
function watchdog_event($id) {
146
  $severity = array(WATCHDOG_NOTICE => t('notice'), WATCHDOG_WARNING => t('warning'), WATCHDOG_ERROR => t('error'));
Dries's avatar
Dries committed
147
148
  $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
149
  if ($watchdog = db_fetch_object($result)) {
Dries's avatar
Dries committed
150
    $output .= '<table border="1" cellpadding="2" cellspacing="2">';
Dries's avatar
   
Dries committed
151
    $output .= ' <tr><th>'. t('Type') .'</th><td>' . t($watchdog->type) . '</td></tr>';
Dries's avatar
Dries committed
152
    $output .= ' <tr><th>'. t('Date') .'</th><td>'. format_date($watchdog->timestamp, 'large') .'</td></tr>';
153
    $output .= ' <tr><th>'. t('User') .'</th><td>'. theme('username', $watchdog) .'</td></tr>';
154
    $output .= ' <tr><th>'. t('Location') ."</th><td>". l($watchdog->location, $watchdog->location) ."</td></tr>";
155
    $output .= ' <tr><th>'. t('Referrer') ."</th><td>". l($watchdog->referer, $watchdog->referer) ."</td></tr>";
Dries's avatar
Dries committed
156
    $output .= ' <tr><th>'. t('Message') ."</th><td>$watchdog->message</td></tr>";
157
    $output .= ' <tr><th>'. t('Severity') .'</th><td>'. $severity[$watchdog->severity] .'</td></tr>';
Dries's avatar
Dries committed
158
159
    $output .= ' <tr><th>'. t('Hostname') ."</th><td>$watchdog->hostname</td></tr>";
    $output .= '</table>';
Dries's avatar
 
Dries committed
160
  }
Dries's avatar
   
Dries committed
161
  return $output;
Dries's avatar
 
Dries committed
162
}
Dries's avatar
   
Dries committed
163

Dries's avatar
   
Dries committed
164
165
166
function _watchdog_get_message_types() {
  $types = array();

167
  $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
Dries's avatar
   
Dries committed
168
169
170
171
172
173
174
  while ($object = db_fetch_object($result)) {
    $types[] = $object->type;
  }

  return $types;
}

175