watchdog.module

  1. drupal
    1. 4.6 modules/watchdog.module
    2. 4.7 modules/watchdog.module
    3. 5 modules/watchdog/watchdog.module

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 also

watchdog().

Functions & methods

NameDescription
theme_watchdog_event
theme_watchdog_form_overview
watchdog_cronImplementation of hook_cron().
watchdog_eventMenu callback; displays details about a log message.
watchdog_form_overview_submit
watchdog_helpImplementation of hook_help().
watchdog_menuImplementation of hook_menu().
watchdog_overviewMenu callback; displays a listing of log messages.
watchdog_userImplementation of hook_user().
_watchdog_get_message_types

File

modules/watchdog.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * System monitoring and logging for administrators.
  5. *
  6. * The watchdog module monitors your site and keeps a list of
  7. * recorded events containing usage and performance data, errors,
  8. * warnings, and similar operational information.
  9. *
  10. * @see watchdog().
  11. */
  12. /**
  13. * Implementation of hook_help().
  14. */
  15. function watchdog_help($section) {
  16. switch ($section) {
  17. case 'admin/help#watchdog':
  18. $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>';
  19. $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>';
  20. $output .= t('<p>You can</p>
  21. <ul>
  22. <li>view watchdog logs at <a href="%admin-watchdog">administer &gt;&gt; watchdog</a>.</li>
  23. <li>view watchdog event logs at <a href="%admin-watchdog-events">administer &gt;&gt; watchdog &gt;&gt; events</a>.</li>
  24. </ul>
  25. ', array('%admin-watchdog' => url('admin/watchdog'), '%admin-watchdog-events' => url('admin/watchdog/events')));
  26. $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>';
  27. return $output;
  28. case 'admin/modules#description':
  29. return t('Logs and records system events.');
  30. case 'admin/logs':
  31. 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>');
  32. }
  33. }
  34. /**
  35. * Implementation of hook_menu().
  36. */
  37. function watchdog_menu($may_cache) {
  38. $items = array();
  39. if ($may_cache) {
  40. $items[] = array('path' => 'admin/logs', 'title' => t('logs'),
  41. 'callback' => 'watchdog_overview');
  42. $items[] = array('path' => 'admin/logs/event', 'title' => t('details'),
  43. 'callback' => 'watchdog_event',
  44. 'type' => MENU_CALLBACK);
  45. }
  46. return $items;
  47. }
  48. /**
  49. * Implementation of hook_cron().
  50. *
  51. * Remove expired log messages and flood control events.
  52. */
  53. function watchdog_cron() {
  54. db_query('DELETE FROM {watchdog} WHERE timestamp < %d', time() - variable_get('watchdog_clear', 604800));
  55. db_query('DELETE FROM {flood} WHERE timestamp < %d', time() - 3600);
  56. }
  57. /**
  58. * Implementation of hook_user().
  59. */
  60. function watchdog_user($op, &$edit, &$user) {
  61. if ($op == 'delete') {
  62. db_query('UPDATE {watchdog} SET uid = 0 WHERE uid = %d', $user->uid);
  63. }
  64. }
  65. /**
  66. * Menu callback; displays a listing of log messages.
  67. */
  68. function watchdog_overview() {
  69. if (ini_get('register_globals')) {
  70. drupal_set_message(t('<em>register_globals</em> is enabled. Drupal requires this configuration directive to be disabled. Your site may not be secure when <em>register_globals</em> is enabled. The PHP manual has instructions for <a href="http://php.net/configuration.changes">how to change configuration settings</a>.'), 'error');
  71. }
  72. $icons = array(WATCHDOG_NOTICE => '',
  73. WATCHDOG_WARNING => theme('image', 'misc/watchdog-warning.png', t('warning'), t('warning')),
  74. WATCHDOG_ERROR => theme('image', 'misc/watchdog-error.png', t('error'), t('error')));
  75. $classes = array(WATCHDOG_NOTICE => 'watchdog-notice', WATCHDOG_WARNING => 'watchdog-warning', WATCHDOG_ERROR => 'watchdog-error');
  76. $names['all'] = t('all messages');
  77. foreach (_watchdog_get_message_types() as $type) {
  78. $names[$type] = t('%type messages', array('%type' => t($type)));
  79. }
  80. if (empty($_SESSION['watchdog_overview_filter'])) {
  81. $_SESSION['watchdog_overview_filter'] = 'all';
  82. }
  83. $form['filter'] = array(
  84. '#type' => 'select',
  85. '#title' => t('Filter by message type'),
  86. '#options' => $names,
  87. '#default_value' => $_SESSION['watchdog_overview_filter']
  88. );
  89. $form['#action'] = url('admin/logs');
  90. $form['submit'] = array('#type' => 'submit', '#value' =>t('Filter'));
  91. $output = drupal_get_form('watchdog_form_overview', $form);
  92. $header = array(
  93. ' ',
  94. array('data' => t('Type'), 'field' => 'w.type'),
  95. array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc'),
  96. array('data' => t('Message'), 'field' => 'w.message'),
  97. array('data' => t('User'), 'field' => 'u.name'),
  98. array('data' => t('Operations'))
  99. );
  100. $sql = "SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid";
  101. $tablesort = tablesort_sql($header);
  102. $type = $_SESSION['watchdog_overview_filter'];
  103. if ($type != 'all') {
  104. $result = pager_query($sql ." WHERE w.type = '%s'". $tablesort, 50, 0, NULL, $type);
  105. }
  106. else {
  107. $result = pager_query($sql . $tablesort, 50);
  108. }
  109. while ($watchdog = db_fetch_object($result)) {
  110. $rows[] = array('data' =>
  111. array(
  112. // Cells
  113. $icons[$watchdog->severity],
  114. t($watchdog->type),
  115. format_date($watchdog->timestamp, 'small'),
  116. l(truncate_utf8($watchdog->message, 56, TRUE, TRUE), 'admin/logs/event/'. $watchdog->wid, array(), NULL, NULL, FALSE, TRUE),
  117. theme('username', $watchdog),
  118. $watchdog->link,
  119. ),
  120. // Attributes for tr
  121. 'class' => "watchdog-". preg_replace('/[^a-z]/i', '-', $watchdog->type) .' '. $classes[$watchdog->severity]
  122. );
  123. }
  124. if (!$rows) {
  125. $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => 6));
  126. }
  127. $output .= theme('table', $header, $rows);
  128. $output .= theme('pager', NULL, 50, 0);
  129. return $output;
  130. }
  131. function theme_watchdog_form_overview($form) {
  132. return '<div class="container-inline">'. form_render($form) .'</div>';
  133. }
  134. function watchdog_form_overview_submit($form_id, $form) {
  135. global $form_values;
  136. $_SESSION['watchdog_overview_filter'] = $form_values['filter'];
  137. }
  138. /**
  139. * Menu callback; displays details about a log message.
  140. */
  141. function watchdog_event($id) {
  142. $severity = array(WATCHDOG_NOTICE => t('notice'), WATCHDOG_WARNING => t('warning'), WATCHDOG_ERROR => t('error'));
  143. $output = '';
  144. $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);
  145. if ($watchdog = db_fetch_object($result)) {
  146. $header = array(t('Type'), t('Date'), t('User'), t('Location'), t('Referrer'), t('Message'), t('Severity'), t('Hostname'));
  147. $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);
  148. $output = theme('watchdog_event', $header, $data);
  149. }
  150. return $output;
  151. }
  152. function theme_watchdog_event($header, $data) {
  153. $output = '';
  154. $output .= '<table class="watchdog-event">';
  155. $n = count($header);
  156. for ($i = 0; $i < $n; $i++) {
  157. $output .= '<tr class="' . ($i % 2 == 0 ? 'even' : 'odd') . '"><th>' . $header[$i] . '</th><td>' . $data[$i] . '</td></tr>';
  158. }
  159. $output .= '</table>';
  160. return $output;
  161. }
  162. function _watchdog_get_message_types() {
  163. $types = array();
  164. $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
  165. while ($object = db_fetch_object($result)) {
  166. $types[] = $object->type;
  167. }
  168. return $types;
  169. }
Login or register to post comments