dblog.module

  1. 7.x drupal-7.x/modules/dblog/dblog.module
  2. 6.x drupal-6.x/modules/dblog/dblog.module

System monitoring and logging for administrators.

The Database Logging 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()

File

drupal-7.x/modules/dblog/dblog.module
View source
  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. Database::getConnection('default', 'default')->insert('watchdog')
  135. ->fields(array(
  136. 'uid' => $log_entry['uid'],
  137. 'type' => substr($log_entry['type'], 0, 64),
  138. 'message' => $log_entry['message'],
  139. 'variables' => serialize($log_entry['variables']),
  140. 'severity' => $log_entry['severity'],
  141. 'link' => substr($log_entry['link'], 0, 255),
  142. 'location' => $log_entry['request_uri'],
  143. 'referer' => $log_entry['referer'],
  144. 'hostname' => substr($log_entry['ip'], 0, 128),
  145. 'timestamp' => $log_entry['timestamp'],
  146. ))
  147. ->execute();
  148. }
  149. /**
  150. * Implements hook_form_FORM_ID_alter() for system_logging_settings().
  151. */
  152. function dblog_form_system_logging_settings_alter(&$form, $form_state) {
  153. $form['dblog_row_limit'] = array(
  154. '#type' => 'select',
  155. '#title' => t('Database log messages to keep'),
  156. '#default_value' => variable_get('dblog_row_limit', 1000),
  157. '#options' => array(0 => t('All')) + drupal_map_assoc(array(100, 1000, 10000, 100000, 1000000)),
  158. '#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')))
  159. );
  160. $form['actions']['#weight'] = 1;
  161. }
  162. /**
  163. * Implements hook_theme().
  164. */
  165. function dblog_theme() {
  166. return array(
  167. 'dblog_message' => array(
  168. 'variables' => array('event' => NULL, 'link' => FALSE),
  169. 'file' => 'dblog.admin.inc',
  170. ),
  171. );
  172. }