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_form_overview
watchdog_cronImplementation of hook_cron().
watchdog_eventMenu callback; displays details about a log message.
watchdog_form_overview
watchdog_form_overview_submit
watchdog_helpImplementation of hook_help().
watchdog_menuImplementation of hook_menu().
watchdog_overviewMenu callback; displays a listing of log messages.
watchdog_topMenu callback; generic function to display a page of the most frequent watchdog events of a specified type.
watchdog_userImplementation of hook_user().
_watchdog_get_message_types

File

modules/watchdog/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 .= '<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>';
  21. return $output;
  22. case 'admin/logs':
  23. return '<p>'. t('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>';
  24. }
  25. }
  26. /**
  27. * Implementation of hook_menu().
  28. */
  29. function watchdog_menu($may_cache) {
  30. $items = array();
  31. if ($may_cache) {
  32. $items[] = array('path' => 'admin/logs/watchdog', 'title' => t('Recent log entries'),
  33. 'description' => t('View events that have recently been logged.'),
  34. 'callback' => 'watchdog_overview',
  35. 'weight' => -1);
  36. $items[] = array('path' => 'admin/logs/page-not-found', 'title' => t("Top 'page not found' errors"),
  37. 'description' => t("View 'page not found' errors (404s)."),
  38. 'callback' => 'watchdog_top',
  39. 'callback arguments' => array('page not found'));
  40. $items[] = array('path' => 'admin/logs/access-denied', 'title' => t("Top 'access denied' errors"),
  41. 'description' => t("View 'access denied' errors (403s)."),
  42. 'callback' => 'watchdog_top',
  43. 'callback arguments' => array('access denied'));
  44. $items[] = array('path' => 'admin/logs/event', 'title' => t('Details'),
  45. 'callback' => 'watchdog_event',
  46. 'type' => MENU_CALLBACK);
  47. }
  48. else {
  49. if (arg(0) == 'admin' && arg(1) == 'logs') {
  50. // Add the CSS for this module
  51. drupal_add_css(drupal_get_path('module', 'watchdog') .'/watchdog.css', 'module', 'all', FALSE);
  52. }
  53. }
  54. return $items;
  55. }
  56. /**
  57. * Implementation of hook_cron().
  58. *
  59. * Remove expired log messages and flood control events.
  60. */
  61. function watchdog_cron() {
  62. db_query('DELETE FROM {watchdog} WHERE timestamp < %d', time() - variable_get('watchdog_clear', 604800));
  63. db_query('DELETE FROM {flood} WHERE timestamp < %d', time() - 3600);
  64. }
  65. /**
  66. * Implementation of hook_user().
  67. */
  68. function watchdog_user($op, &$edit, &$user) {
  69. if ($op == 'delete') {
  70. db_query('UPDATE {watchdog} SET uid = 0 WHERE uid = %d', $user->uid);
  71. }
  72. }
  73. function watchdog_form_overview() {
  74. $names['all'] = t('all messages');
  75. foreach (_watchdog_get_message_types() as $type) {
  76. $names[$type] = t('!type messages', array('!type' => t($type)));
  77. }
  78. if (empty($_SESSION['watchdog_overview_filter'])) {
  79. $_SESSION['watchdog_overview_filter'] = 'all';
  80. }
  81. $form['filter'] = array(
  82. '#type' => 'select',
  83. '#title' => t('Filter by message type'),
  84. '#options' => $names,
  85. '#default_value' => $_SESSION['watchdog_overview_filter']
  86. );
  87. $form['submit'] = array('#type' => 'submit', '#value' => t('Filter'));
  88. $form['#redirect'] = FALSE;
  89. return $form;
  90. }
  91. /**
  92. * Menu callback; displays a listing of log messages.
  93. */
  94. function watchdog_overview() {
  95. $icons = array(WATCHDOG_NOTICE => '',
  96. WATCHDOG_WARNING => theme('image', 'misc/watchdog-warning.png', t('warning'), t('warning')),
  97. WATCHDOG_ERROR => theme('image', 'misc/watchdog-error.png', t('error'), t('error')));
  98. $classes = array(WATCHDOG_NOTICE => 'watchdog-notice', WATCHDOG_WARNING => 'watchdog-warning', WATCHDOG_ERROR => 'watchdog-error');
  99. $output = drupal_get_form('watchdog_form_overview');
  100. $header = array(
  101. ' ',
  102. array('data' => t('Type'), 'field' => 'w.type'),
  103. array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc'),
  104. array('data' => t('Message'), 'field' => 'w.message'),
  105. array('data' => t('User'), 'field' => 'u.name'),
  106. array('data' => t('Operations'))
  107. );
  108. $sql = "SELECT w.wid, w.uid, w.severity, w.type, w.timestamp, w.message, w.link, u.name FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid";
  109. $tablesort = tablesort_sql($header);
  110. $type = $_SESSION['watchdog_overview_filter'];
  111. if ($type != 'all') {
  112. $result = pager_query($sql ." WHERE w.type = '%s'". $tablesort, 50, 0, NULL, $type);
  113. }
  114. else {
  115. $result = pager_query($sql . $tablesort, 50);
  116. }
  117. while ($watchdog = db_fetch_object($result)) {
  118. $rows[] = array('data' =>
  119. array(
  120. // Cells
  121. $icons[$watchdog->severity],
  122. t($watchdog->type),
  123. format_date($watchdog->timestamp, 'small'),
  124. l(truncate_utf8($watchdog->message, 56, TRUE, TRUE), 'admin/logs/event/'. $watchdog->wid, array(), NULL, NULL, FALSE, TRUE),
  125. theme('username', $watchdog),
  126. $watchdog->link,
  127. ),
  128. // Attributes for tr
  129. 'class' => "watchdog-". preg_replace('/[^a-z]/i', '-', $watchdog->type) .' '. $classes[$watchdog->severity]
  130. );
  131. }
  132. if (!$rows) {
  133. $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => 6));
  134. }
  135. $output .= theme('table', $header, $rows);
  136. $output .= theme('pager', NULL, 50, 0);
  137. return $output;
  138. }
  139. /**
  140. * Menu callback; generic function to display a page of the most frequent
  141. * watchdog events of a specified type.
  142. */
  143. function watchdog_top($type) {
  144. $header = array(
  145. array('data' => t('Count'), 'field' => 'count', 'sort' => 'desc'),
  146. array('data' => t('Message'), 'field' => 'message')
  147. );
  148. $result = pager_query("SELECT COUNT(wid) AS count, message FROM {watchdog} WHERE type = '%s' GROUP BY message ". tablesort_sql($header), 30, 0, "SELECT COUNT(DISTINCT(message)) FROM {watchdog} WHERE type = '%s'", $type);
  149. while ($watchdog = db_fetch_object($result)) {
  150. $rows[] = array($watchdog->count, truncate_utf8($watchdog->message, 56, TRUE, TRUE));
  151. }
  152. if (!$rows) {
  153. $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => 2));
  154. }
  155. $output = theme('table', $header, $rows);
  156. $output .= theme('pager', NULL, 30, 0);
  157. return $output;
  158. }
  159. function theme_watchdog_form_overview($form) {
  160. return '<div class="container-inline">'. drupal_render($form) .'</div>';
  161. }
  162. function watchdog_form_overview_submit($form_id, $form_values) {
  163. $_SESSION['watchdog_overview_filter'] = $form_values['filter'];
  164. }
  165. /**
  166. * Menu callback; displays details about a log message.
  167. */
  168. function watchdog_event($id) {
  169. $severity = array(WATCHDOG_NOTICE => t('notice'), WATCHDOG_WARNING => t('warning'), WATCHDOG_ERROR => t('error'));
  170. $output = '';
  171. $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);
  172. if ($watchdog = db_fetch_object($result)) {
  173. $rows = array(
  174. array(
  175. array('data' => t('Type'), 'header' => TRUE),
  176. t($watchdog->type),
  177. ),
  178. array(
  179. array('data' => t('Date'), 'header' => TRUE),
  180. format_date($watchdog->timestamp, 'large'),
  181. ),
  182. array(
  183. array('data' => t('User'), 'header' => TRUE),
  184. theme('username', $watchdog),
  185. ),
  186. array(
  187. array('data' => t('Location'), 'header' => TRUE),
  188. l($watchdog->location, $watchdog->location),
  189. ),
  190. array(
  191. array('data' => t('Referrer'), 'header' => TRUE),
  192. l($watchdog->referer, $watchdog->referer),
  193. ),
  194. array(
  195. array('data' => t('Message'), 'header' => TRUE),
  196. $watchdog->message,
  197. ),
  198. array(
  199. array('data' => t('Severity'), 'header' => TRUE),
  200. $severity[$watchdog->severity],
  201. ),
  202. array(
  203. array('data' => t('Hostname'), 'header' => TRUE),
  204. $watchdog->hostname,
  205. ),
  206. );
  207. $attributes = array('class' => 'watchdog-event');
  208. $output = theme('table', array(), $rows, $attributes);
  209. }
  210. return $output;
  211. }
  212. function _watchdog_get_message_types() {
  213. $types = array();
  214. $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
  215. while ($object = db_fetch_object($result)) {
  216. $types[] = $object->type;
  217. }
  218. return $types;
  219. }
Login or register to post comments