watchdog.module 7.23 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
/**
 * Implementation of hook_help().
 */
18
function watchdog_help($section) {
Dries's avatar
   
Dries committed
19
  switch ($section) {
20
21
22
23
24
25
26
27
28
29
30
    case 'admin/help#watchdog':
      $output = '<p>'. t('The watchdog module monitors your system, capturing system events in a log to be reviewed by an authorized individual at a later time.  This is useful for site administrators who want a quick overview of activities on their site. The logs also record the sequence of events, so it can be useful for debugging site errors.') .'</p>';
      $output .= '<p>'. t('The watchdog log is simply a list of recorded events containing usage data, performance data, errors, warnings and operational information. Administrators should check the watchdog report on a regular basis to ensure their site is working properly.') .'</p>';
      $output .= t('<p>You can</p>
<ul>
<li>view watchdog logs at <a href="%admin-watchdog">administer &gt;&gt; watchdog</a>.</li>
<li>view watchdog event logs at <a href="%admin-watchdog-events">administer &gt;&gt; watchdog &gt;&gt; events</a>.</li>
</ul>
', array('%admin-watchdog' => url('admin/watchdog'), '%admin-watchdog-events' => url('admin/watchdog/events')));
      $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%watchdog">Watchdog page</a>.', array('%watchdog' => 'http://www.drupal.org/handbook/modules/watchdog/')) .'</p>';
      return $output;
Dries's avatar
   
Dries committed
31
    case 'admin/modules#description':
Dries's avatar
Dries committed
32
      return t('Logs and records system events.');
33
34
    case 'admin/logs':
      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
35
  }
36
37
}

Dries's avatar
   
Dries committed
38
/**
Dries's avatar
   
Dries committed
39
 * Implementation of hook_menu().
Dries's avatar
   
Dries committed
40
 */
Dries's avatar
   
Dries committed
41
function watchdog_menu($may_cache) {
Dries's avatar
   
Dries committed
42
  $items = array();
Dries's avatar
   
Dries committed
43

Dries's avatar
   
Dries committed
44
45
  if ($may_cache) {
    $items[] = array('path' => 'admin/logs', 'title' => t('logs'),
46
      'callback' => 'watchdog_overview');
47
    $items[] = array('path' => 'admin/logs/event', 'title' => t('details'),
48
      'callback' => 'watchdog_event',
Dries's avatar
   
Dries committed
49
      'type' => MENU_CALLBACK);
Dries's avatar
   
Dries committed
50
  }
Dries's avatar
   
Dries committed
51
  return $items;
Dries's avatar
   
Dries committed
52
53
}

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

Dries's avatar
Dries committed
64
65
66
/**
 * Menu callback; displays a listing of log messages.
 */
67
function watchdog_overview() {
68
69
70
71
72
  $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');

73
74
  $names['all'] = t('all messages');
  foreach (_watchdog_get_message_types() as $type) {
Dries's avatar
   
Dries committed
75
    $names[$type] = t('%type messages', array('%type' => t($type)));
76
77
78
79
80
81
  }

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

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

86
  $form['filter'] = array(
87
88
89
90
    '#type' => 'select',
    '#title' => t('Filter by message type'),
    '#options' => $names,
    '#default_value' => $_SESSION['watchdog_overview_filter']
91
  );
92
  $form['#action'] = url('admin/logs');
93

94
  $form['submit'] = array('#type' => 'submit', '#value' =>t('Filter'));
95
  $output = drupal_get_form('watchdog_form_overview', $form);
96

Dries's avatar
   
Dries committed
97
  $header = array(
98
    ' ',
99
    array('data' => t('Type'), 'field' => 'w.type'),
100
    array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc'),
Dries's avatar
   
Dries committed
101
102
    array('data' => t('Message'), 'field' => 'w.message'),
    array('data' => t('User'), 'field' => 'u.name'),
103
    array('data' => t('Operations'))
Dries's avatar
   
Dries committed
104
  );
105
106
107
108
109
110
111
112
113
114

  $sql = "SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid";
  $tablesort = tablesort_sql($header);
  $type = $_SESSION['watchdog_overview_filter'];
  if ($type != 'all') {
    $result = pager_query($sql ." WHERE w.type = '%s'". $tablesort, 50, 0, NULL, $type);
  }
  else {
    $result = pager_query($sql . $tablesort, 50);
  }
Dries's avatar
   
Dries committed
115

Dries's avatar
   
Dries committed
116
  while ($watchdog = db_fetch_object($result)) {
117
118
119
    $rows[] = array('data' =>
      array(
        // Cells
120
        $icons[$watchdog->severity],
Dries's avatar
   
Dries committed
121
        t($watchdog->type),
122
        format_date($watchdog->timestamp, 'small'),
123
        l(truncate_utf8($watchdog->message, 56, TRUE, TRUE), 'admin/logs/event/'. $watchdog->wid, array(), NULL, NULL, FALSE, TRUE),
124
        theme('username', $watchdog),
125
126
127
        $watchdog->link,
      ),
      // Attributes for tr
128
      'class' => "watchdog-". preg_replace('/[^a-z]/i', '-', $watchdog->type) .' '. $classes[$watchdog->severity]
Dries's avatar
   
Dries committed
129
    );
Dries's avatar
   
Dries committed
130
  }
Dries's avatar
   
Dries committed
131

Dries's avatar
   
Dries committed
132
  if (!$rows) {
133
    $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => 6));
Dries's avatar
   
Dries committed
134
  }
Dries's avatar
   
Dries committed
135

136
  $output .= theme('table', $header, $rows);
137
  $output .= theme('pager', NULL, 50, 0, tablesort_pager());
138

Dries's avatar
   
Dries committed
139
  return $output;
Dries's avatar
   
Dries committed
140
141
}

142
143
144
145
function theme_watchdog_form_overview($form) {
  return '<div class="container-inline">'. form_render($form) .'</div>';
}

146
function watchdog_form_overview_submit($form_id, $form) {
147
148
149
150
  global $form_values;
  $_SESSION['watchdog_overview_filter'] = $form_values['filter'];
}

Dries's avatar
Dries committed
151
152
153
/**
 * Menu callback; displays details about a log message.
 */
154
function watchdog_event($id) {
155
  $severity = array(WATCHDOG_NOTICE => t('notice'), WATCHDOG_WARNING => t('warning'), WATCHDOG_ERROR => t('error'));
Dries's avatar
Dries committed
156
157
  $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
158
  if ($watchdog = db_fetch_object($result)) {
159
160
161
    $header = array(t('Type'), t('Date'), t('User'), t('Location'), t('Referrer'), t('Message'), t('Severity'), t('Hostname'));
    $data = array(t($watchdog->type), format_date($watchdog->timestamp, 'large'), theme('username', $watchdog), l($watchdog->location, $watchdog->location), l($watchdog->referer, $watchdog->referer), $watchdog->message, $severity[$watchdog->severity], $watchdog->hostname);
    $output = theme('watchdog_event', $header, $data);
Dries's avatar
   
Dries committed
162
  }
Dries's avatar
   
Dries committed
163
  return $output;
Dries's avatar
   
Dries committed
164
}
Dries's avatar
   
Dries committed
165

166
167
168
169
170
171
172
173
174
175
176
177
178
179
function theme_watchdog_event($header, $data) {
  $output = '';
  $output .= '<table class="watchdog-event">';

  $n = count($header);
  for ($i = 0; $i < $n; $i++) {
    $output .= '<tr class="' . ($i % 2 == 0 ? 'even' : 'odd') . '"><th>' . $header[$i] . '</th><td>' . $data[$i] . '</td></tr>';
  }

  $output .= '</table>';

  return $output;
}

Dries's avatar
   
Dries committed
180
181
182
function _watchdog_get_message_types() {
  $types = array();

183
  $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
Dries's avatar
   
Dries committed
184
185
186
187
188
189
190
  while ($object = db_fetch_object($result)) {
    $types[] = $object->type;
  }

  return $types;
}

191