dblog.module 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * @file
  4. * System monitoring and logging for administrators.
  5. *
  6. * The dblog 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. * Implements hook_help().
  14. */
  15. function dblog_help($path, $arg) {
  16. switch ($path) {
  17. case 'admin/help#dblog':
  18. $output = '';
  19. $output .= '<h3>' . t('About') . '</h3>';
  20. $output .= '<p>' . t('The Database logging module logs system events in the Drupal database. For more information, see the online handbook entry for the <a href="@dblog">Database logging module</a>.', array('@dblog' => 'http://drupal.org/documentation/modules/dblog')) . '</p>';
  21. $output .= '<h3>' . t('Uses') . '</h3>';
  22. $output .= '<dl>';
  23. $output .= '<dt>' . t('Monitoring your site') . '</dt>';
  24. $output .= '<dd>' . t('The Database logging module allows you to view an event log on the <a href="@dblog">Recent log messages</a> page. The log is a chronological list of recorded events containing usage data, performance data, errors, warnings and operational information. Administrators should check the log on a regular basis to ensure their site is working properly.', array('@dblog' => url('admin/reports/dblog'))) . '</dd>';
  25. $output .= '<dt>' . t('Debugging site problems') . '</dt>';
  26. $output .= '<dd>' . t('In case of errors or problems with the site, the <a href="@dblog">Recent log messages</a> page can be useful for debugging, since it shows the sequence of events. The log messages include usage information, warnings, and errors.', array('@dblog' => url('admin/reports/dblog'))) . '</dd>';
  27. $output .= '</dl>';
  28. return $output;
  29. case 'admin/reports/dblog':
  30. return '<p>' . t('The Database logging module monitors your website, capturing system events in a log (shown here) to be reviewed by an authorized individual at a later time. This log is a list of recorded events containing usage data, performance data, errors, warnings and operational information. It is vital to check the Recent log messages report on a regular basis, as it is often the only way to tell what is going on.') . '</p>';
  31. }
  32. }
  33. /**
  34. * Implements hook_menu().
  35. */
  36. function dblog_menu() {
  37. $items['admin/reports/dblog'] = array(
  38. 'title' => 'Recent log messages',
  39. 'description' => 'View events that have recently been logged.',
  40. 'page callback' => 'dblog_overview',
  41. 'access arguments' => array('access site reports'),
  42. 'weight' => -1,
  43. 'file' => 'dblog.admin.inc',
  44. );
  45. $items['admin/reports/page-not-found'] = array(
  46. 'title' => "Top 'page not found' errors",
  47. 'description' => "View 'page not found' errors (404s).",
  48. 'page callback' => 'dblog_top',
  49. 'page arguments' => array('page not found'),
  50. 'access arguments' => array('access site reports'),
  51. 'file' => 'dblog.admin.inc',
  52. );
  53. $items['admin/reports/access-denied'] = array(
  54. 'title' => "Top 'access denied' errors",
  55. 'description' => "View 'access denied' errors (403s).",
  56. 'page callback' => 'dblog_top',
  57. 'page arguments' => array('access denied'),
  58. 'access arguments' => array('access site reports'),
  59. 'file' => 'dblog.admin.inc',
  60. );
  61. $items['admin/reports/event/%'] = array(
  62. 'title' => 'Details',
  63. 'page callback' => 'dblog_event',
  64. 'page arguments' => array(3),
  65. 'access arguments' => array('access site reports'),
  66. 'file' => 'dblog.admin.inc',
  67. );
  68. if (module_exists('search')) {
  69. $items['admin/reports/search'] = array(
  70. 'title' => 'Top search phrases',
  71. 'description' => 'View most popular search phrases.',
  72. 'page callback' => 'dblog_top',
  73. 'page arguments' => array('search'),
  74. 'access arguments' => array('access site reports'),
  75. 'file' => 'dblog.admin.inc',
  76. );
  77. }
  78. return $items;
  79. }
  80. /**
  81. * Implements hook_init().
  82. */
  83. function dblog_init() {
  84. if (arg(0) == 'admin' && arg(1) == 'reports') {
  85. // Add the CSS for this module
  86. drupal_add_css(drupal_get_path('module', 'dblog') . '/dblog.css');
  87. }
  88. }
  89. /**
  90. * Implements hook_cron().
  91. *
  92. * Remove expired log messages.
  93. */
  94. function dblog_cron() {
  95. // Cleanup the watchdog table.
  96. $row_limit = variable_get('dblog_row_limit', 1000);
  97. // For row limit n, get the wid of the nth row in descending wid order.
  98. // Counting the most recent n rows avoids issues with wid number sequences,
  99. // e.g. auto_increment value > 1 or rows deleted directly from the table.
  100. if ($row_limit > 0) {
  101. $min_row = db_select('watchdog', 'w')
  102. ->fields('w', array('wid'))
  103. ->orderBy('wid', 'DESC')
  104. ->range($row_limit - 1, 1)
  105. ->execute()->fetchField();
  106. // Delete all table entries older than the nth row, if nth row was found.
  107. if ($min_row) {
  108. db_delete('watchdog')
  109. ->condition('wid', $min_row, '<')
  110. ->execute();
  111. }
  112. }
  113. }
  114. function _dblog_get_message_types() {
  115. $types = array();
  116. $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
  117. foreach ($result as $object) {
  118. $types[] = $object->type;
  119. }
  120. return $types;
  121. }
  122. /**
  123. * Implements hook_watchdog().
  124. *
  125. * Note some values may be truncated for database column size restrictions.
  126. */
  127. function dblog_watchdog(array $log_entry) {
  128. Database::getConnection('default', 'default')->insert('watchdog')
  129. ->fields(array(
  130. 'uid' => $log_entry['uid'],
  131. 'type' => substr($log_entry['type'], 0, 64),
  132. 'message' => $log_entry['message'],
  133. 'variables' => serialize($log_entry['variables']),
  134. 'severity' => $log_entry['severity'],
  135. 'link' => substr($log_entry['link'], 0, 255),
  136. 'location' => $log_entry['request_uri'],
  137. 'referer' => $log_entry['referer'],
  138. 'hostname' => substr($log_entry['ip'], 0, 128),
  139. 'timestamp' => $log_entry['timestamp'],
  140. ))
  141. ->execute();
  142. }
  143. /**
  144. * Implements hook_form_FORM_ID_alter().
  145. */
  146. function dblog_form_system_logging_settings_alter(&$form, $form_state) {
  147. $form['dblog_row_limit'] = array(
  148. '#type' => 'select',
  149. '#title' => t('Database log messages to keep'),
  150. '#default_value' => variable_get('dblog_row_limit', 1000),
  151. '#options' => array(0 => t('All')) + drupal_map_assoc(array(100, 1000, 10000, 100000, 1000000)),
  152. '#description' => t('The maximum number of messages to keep in the database log. Requires a <a href="@cron">cron maintenance task</a>.', array('@cron' => url('admin/reports/status')))
  153. );
  154. $form['actions']['#weight'] = 1;
  155. }
  156. /**
  157. * Implements hook_theme().
  158. */
  159. function dblog_theme() {
  160. return array(
  161. 'dblog_message' => array(
  162. 'variables' => array('event' => NULL, 'link' => FALSE),
  163. 'file' => 'dblog.admin.inc',
  164. ),
  165. );
  166. }