dblog.module 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * @file
  4. * System monitoring and logging for administrators.
  5. *
  6. * The Database Logging module monitors your site and keeps a list of recorded
  7. * events containing usage and performance data, errors, warnings, and similar
  8. * 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. * Controls the size of the log table, paring it to 'dblog_row_limit' 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. /**
  115. * Gathers a list of uniquely defined database log message types.
  116. *
  117. * @return array
  118. * List of uniquely defined database log message types.
  119. */
  120. function _dblog_get_message_types() {
  121. $types = array();
  122. $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
  123. foreach ($result as $object) {
  124. $types[] = $object->type;
  125. }
  126. return $types;
  127. }
  128. /**
  129. * Implements hook_watchdog().
  130. *
  131. * Note: Some values may be truncated to meet database column size restrictions.
  132. */
  133. function dblog_watchdog(array $log_entry) {
  134. if (!function_exists('drupal_substr')) {
  135. require_once DRUPAL_ROOT . '/includes/unicode.inc';
  136. }
  137. try {
  138. Database::getConnection('default', 'default')->insert('watchdog')
  139. ->fields(array(
  140. 'uid' => $log_entry['uid'],
  141. 'type' => drupal_substr($log_entry['type'], 0, 64),
  142. 'message' => $log_entry['message'],
  143. 'variables' => serialize($log_entry['variables']),
  144. 'severity' => $log_entry['severity'],
  145. 'link' => drupal_substr($log_entry['link'], 0, 255),
  146. 'location' => $log_entry['request_uri'],
  147. 'referer' => $log_entry['referer'],
  148. 'hostname' => drupal_substr($log_entry['ip'], 0, 128),
  149. 'timestamp' => $log_entry['timestamp'],
  150. ))
  151. ->execute();
  152. }
  153. catch (Exception $e) {
  154. // Exception is ignored so that watchdog does not break pages during the
  155. // installation process or is not able to create the watchdog table during
  156. // installation.
  157. }
  158. }
  159. /**
  160. * Implements hook_form_FORM_ID_alter() for system_logging_settings().
  161. */
  162. function dblog_form_system_logging_settings_alter(&$form, $form_state) {
  163. $form['dblog_row_limit'] = array(
  164. '#type' => 'select',
  165. '#title' => t('Database log messages to keep'),
  166. '#default_value' => variable_get('dblog_row_limit', 1000),
  167. '#options' => array(0 => t('All')) + drupal_map_assoc(array(100, 1000, 10000, 100000, 1000000)),
  168. '#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')))
  169. );
  170. $form['actions']['#weight'] = 1;
  171. }
  172. /**
  173. * Implements hook_theme().
  174. */
  175. function dblog_theme() {
  176. return array(
  177. 'dblog_message' => array(
  178. 'variables' => array('event' => NULL, 'link' => FALSE),
  179. 'file' => 'dblog.admin.inc',
  180. ),
  181. );
  182. }