watchdog.module 5.84 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
80
81
82
    $queries[$type] = "WHERE type = '". db_escape_string($type) ."'";
  }

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

  $op = $_POST['op'];
  if ($op == t('Filter') && isset($_POST['edit']['filter'])) {
    $_SESSION['watchdog_overview_filter'] = $_POST['edit']['filter'];
Dries's avatar
   
Dries committed
83
  }
Dries's avatar
 
Dries committed
84

Dries's avatar
   
Dries committed
85
  $form  = form_select(t('Filter by message type'), 'filter', $_SESSION['watchdog_overview_filter'], $names);
86
87
  $form .= form_submit(t('Filter'));

Dries's avatar
   
Dries committed
88
  $header = array(
89
    ' ',
90
    array('data' => t('Type'), 'field' => 'w.type'),
91
    array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc'),
Dries's avatar
   
Dries committed
92
93
94
    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
95
  );
96
  $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
97
  $result = pager_query($sql, 50);
Dries's avatar
   
Dries committed
98

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

Dries's avatar
   
Dries committed
116
  if (!$rows) {
117
    $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => '7'));
Dries's avatar
   
Dries committed
118
  }
Dries's avatar
   
Dries committed
119

120
121
  $output  = '<div class="container-inline">'. form($form) .'</div>';
  $output .= theme('table', $header, $rows);
122
  $output .= theme('pager', NULL, 50, 0, tablesort_pager());
123

Dries's avatar
   
Dries committed
124
  return $output;
Dries's avatar
 
Dries committed
125
126
}

Dries's avatar
Dries committed
127
128
129
/**
 * Menu callback; displays details about a log message.
 */
130
function watchdog_event($id) {
131
  $severity = array(WATCHDOG_NOTICE => t('notice'), WATCHDOG_WARNING => t('warning'), WATCHDOG_ERROR => t('error'));
Dries's avatar
Dries committed
132
133
  $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
134
  if ($watchdog = db_fetch_object($result)) {
Dries's avatar
Dries committed
135
    $output .= '<table border="1" cellpadding="2" cellspacing="2">';
Dries's avatar
   
Dries committed
136
    $output .= ' <tr><th>'. t('Type') .'</th><td>' . t($watchdog->type) . '</td></tr>';
Dries's avatar
Dries committed
137
    $output .= ' <tr><th>'. t('Date') .'</th><td>'. format_date($watchdog->timestamp, 'large') .'</td></tr>';
138
    $output .= ' <tr><th>'. t('User') .'</th><td>'. theme('username', $watchdog) .'</td></tr>';
139
    $output .= ' <tr><th>'. t('Location') ."</th><td>". l($watchdog->location, $watchdog->location) ."</td></tr>";
140
    $output .= ' <tr><th>'. t('Referrer') ."</th><td>". l($watchdog->referer, $watchdog->referer) ."</td></tr>";
Dries's avatar
Dries committed
141
    $output .= ' <tr><th>'. t('Message') ."</th><td>$watchdog->message</td></tr>";
142
    $output .= ' <tr><th>'. t('Severity') .'</th><td>'. $severity[$watchdog->severity] .'</td></tr>';
Dries's avatar
Dries committed
143
144
    $output .= ' <tr><th>'. t('Hostname') ."</th><td>$watchdog->hostname</td></tr>";
    $output .= '</table>';
Dries's avatar
 
Dries committed
145
  }
Dries's avatar
   
Dries committed
146
  return $output;
Dries's avatar
 
Dries committed
147
}
Dries's avatar
   
Dries committed
148

Dries's avatar
   
Dries committed
149
150
151
function _watchdog_get_message_types() {
  $types = array();

152
  $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
Dries's avatar
   
Dries committed
153
154
155
156
157
158
159
  while ($object = db_fetch_object($result)) {
    $types[] = $object->type;
  }

  return $types;
}

160