2 // $Id: watchdog.module 144 2007-03-28 07:52:20Z thierry $
6 * System monitoring and logging for administrators.
8 * The watchdog module monitors your site and keeps a list of
9 * recorded events containing usage and performance data, errors,
10 * warnings, and similar operational information.
16 * Implementation of hook_help().
18 function watchdog_help($section) {
20 case 'admin/help#watchdog':
21 $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>';
22 $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>';
23 $output .= t('<p>You can</p>
25 <li>view watchdog logs at <a href="%admin-watchdog">administer >> watchdog</a>.</li>
26 <li>view watchdog event logs at <a href="%admin-watchdog-events">administer >> watchdog >> events</a>.</li>
28 ', array('%admin-watchdog' => url('admin/watchdog'), '%admin-watchdog-events' => url('admin/watchdog/events')));
29 $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%watchdog">Watchdog page</a>.', array('%watchdog' => 'http://drupal.org/handbook/modules/watchdog/')) .'</p>';
31 case 'admin/modules#description':
32 return t('Logs and records system events.');
34 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>');
39 * Implementation of hook_menu().
41 function watchdog_menu($may_cache) {
45 $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',
49 'type' => MENU_CALLBACK);
55 * Implementation of hook_cron().
57 * Remove expired log messages and flood control events.
59 function watchdog_cron() {
60 db_query('DELETE FROM {watchdog} WHERE timestamp < %d', time() - variable_get('watchdog_clear', 604800));
61 db_query('DELETE FROM {flood} WHERE timestamp < %d', time() - 3600);
65 * Implementation of hook_user().
67 function watchdog_user($op, &$edit, &$user) {
68 if ($op == 'delete') {
69 db_query('UPDATE {watchdog} SET uid = 0 WHERE uid = %d', $user->uid);
74 * Menu callback; displays a listing of log messages.
76 function watchdog_overview() {
77 $icons = array(WATCHDOG_NOTICE => '',
78 WATCHDOG_WARNING => theme('image', 'misc/watchdog-warning.png', t('warning'), t('warning')),
79 WATCHDOG_ERROR => theme('image', 'misc/watchdog-error.png', t('error'), t('error')));
80 $classes = array(WATCHDOG_NOTICE => 'watchdog-notice', WATCHDOG_WARNING => 'watchdog-warning', WATCHDOG_ERROR => 'watchdog-error');
82 $names['all'] = t('all messages');
83 foreach (_watchdog_get_message_types() as $type) {
84 $names[$type] = t('%type messages', array('%type' => t($type)));
87 if (empty($_SESSION['watchdog_overview_filter'])) {
88 $_SESSION['watchdog_overview_filter'] = 'all';
91 $form['filter'] = array(
93 '#title' => t('Filter by message type'),
95 '#default_value' => $_SESSION['watchdog_overview_filter']
97 $form['#action'] = url('admin/logs');
99 $form['submit'] = array('#type' => 'submit', '#value' =>t('Filter'));
100 $output = drupal_get_form('watchdog_form_overview', $form);
104 array('data' => t('Type'), 'field' => 'w.type'),
105 array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc'),
106 array('data' => t('Message'), 'field' => 'w.message'),
107 array('data' => t('User'), 'field' => 'u.name'),
108 array('data' => t('Operations'))
111 $sql = "SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid";
112 $tablesort = tablesort_sql($header);
113 $type = $_SESSION['watchdog_overview_filter'];
114 if ($type != 'all') {
115 $result = pager_query($sql ." WHERE w.type = '%s'". $tablesort, 50, 0, NULL, $type);
118 $result = pager_query($sql . $tablesort, 50);
121 while ($watchdog = db_fetch_object($result)) {
122 $rows[] = array('data' =>
125 $icons[$watchdog->severity],
127 format_date($watchdog->timestamp, 'small'),
128 l(truncate_utf8($watchdog->message, 56, TRUE, TRUE), 'admin/logs/event/'. $watchdog->wid, array(), NULL, NULL, FALSE, TRUE),
129 theme('username', $watchdog),
133 'class' => "watchdog-". preg_replace('/[^a-z]/i', '-', $watchdog->type) .' '. $classes[$watchdog->severity]
138 $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => 6));
141 $output .= theme('table', $header, $rows);
142 $output .= theme('pager', NULL, 50, 0);
147 function theme_watchdog_form_overview($form) {
148 return '<div class="container-inline">'. form_render($form) .'</div>';
151 function watchdog_form_overview_submit($form_id, $form) {
153 $_SESSION['watchdog_overview_filter'] = $form_values['filter'];
157 * Menu callback; displays details about a log message.
159 function watchdog_event($id) {
160 $severity = array(WATCHDOG_NOTICE => t('notice'), WATCHDOG_WARNING => t('warning'), WATCHDOG_ERROR => t('error'));
162 $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);
163 if ($watchdog = db_fetch_object($result)) {
164 $header = array(t('Type'), t('Date'), t('User'), t('Location'), t('Referrer'), t('Message'), t('Severity'), t('Hostname'));
165 $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);
166 $output = theme('watchdog_event', $header, $data);
171 function theme_watchdog_event($header, $data) {
173 $output .= '<table class="watchdog-event">';
176 for ($i = 0; $i < $n; $i++) {
177 $output .= '<tr class="' . ($i % 2 == 0 ? 'even' : 'odd') . '"><th>' . $header[$i] . '</th><td>' . $data[$i] . '</td></tr>';
180 $output .= '</table>';
185 function _watchdog_get_message_types() {
188 $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
189 while ($object = db_fetch_object($result)) {
190 $types[] = $object->type;