5 * System monitoring and logging for administrators.
7 * The Database Logging module monitors your site and keeps a list of recorded
8 * events containing usage and performance data, errors, warnings, and similar
9 * operational information.
15 * Implements hook_help().
17 function dblog_help($path, $arg) {
19 case 'admin/help#dblog':
21 $output .= '<h3>' . t('About') . '</h3>';
22 $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>';
23 $output .= '<h3>' . t('Uses') . '</h3>';
25 $output .= '<dt>' . t('Monitoring your site') . '</dt>';
26 $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>';
27 $output .= '<dt>' . t('Debugging site problems') . '</dt>';
28 $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>';
31 case 'admin/reports/dblog':
32 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>';
37 * Implements hook_menu().
39 function dblog_menu() {
40 $items['admin/reports/dblog'] = array(
41 'title' => 'Recent log messages',
42 'description' => 'View events that have recently been logged.',
43 'page callback' => 'dblog_overview',
44 'access arguments' => array('access site reports'),
46 'file' => 'dblog.admin.inc',
48 $items['admin/reports/page-not-found'] = array(
49 'title' => "Top 'page not found' errors",
50 'description' => "View 'page not found' errors (404s).",
51 'page callback' => 'dblog_top',
52 'page arguments' => array('page not found'),
53 'access arguments' => array('access site reports'),
54 'file' => 'dblog.admin.inc',
56 $items['admin/reports/access-denied'] = array(
57 'title' => "Top 'access denied' errors",
58 'description' => "View 'access denied' errors (403s).",
59 'page callback' => 'dblog_top',
60 'page arguments' => array('access denied'),
61 'access arguments' => array('access site reports'),
62 'file' => 'dblog.admin.inc',
64 $items['admin/reports/event/%'] = array(
66 'page callback' => 'dblog_event',
67 'page arguments' => array(3),
68 'access arguments' => array('access site reports'),
69 'file' => 'dblog.admin.inc',
72 if (module_exists('search')) {
73 $items['admin/reports/search'] = array(
74 'title' => 'Top search phrases',
75 'description' => 'View most popular search phrases.',
76 'page callback' => 'dblog_top',
77 'page arguments' => array('search'),
78 'access arguments' => array('access site reports'),
79 'file' => 'dblog.admin.inc',
87 * Implements hook_init().
89 function dblog_init() {
90 if (arg(0) == 'admin' && arg(1) == 'reports') {
91 // Add the CSS for this module
92 drupal_add_css(drupal_get_path('module', 'dblog') . '/dblog.css');
97 * Implements hook_cron().
99 * Controls the size of the log table, paring it to 'dblog_row_limit' messages.
101 function dblog_cron() {
102 // Cleanup the watchdog table.
103 $row_limit = variable_get('dblog_row_limit', 1000);
105 // For row limit n, get the wid of the nth row in descending wid order.
106 // Counting the most recent n rows avoids issues with wid number sequences,
107 // e.g. auto_increment value > 1 or rows deleted directly from the table.
108 if ($row_limit > 0) {
109 $min_row = db_select('watchdog', 'w')
110 ->fields('w', array('wid'))
111 ->orderBy('wid', 'DESC')
112 ->range($row_limit - 1, 1)
113 ->execute()->fetchField();
115 // Delete all table entries older than the nth row, if nth row was found.
117 db_delete('watchdog')
118 ->condition('wid', $min_row, '<')
125 * Gathers a list of uniquely defined database log message types.
128 * List of uniquely defined database log message types.
130 function _dblog_get_message_types() {
133 $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
134 foreach ($result as $object) {
135 $types[] = $object->type;
142 * Implements hook_watchdog().
144 * Note: Some values may be truncated to meet database column size restrictions.
146 function dblog_watchdog(array $log_entry) {
147 if (!function_exists('drupal_substr')) {
148 require_once DRUPAL_ROOT . '/includes/unicode.inc';
151 Database::getConnection('default', 'default')->insert('watchdog')
153 'uid' => $log_entry['uid'],
154 'type' => drupal_substr($log_entry['type'], 0, 64),
155 'message' => $log_entry['message'],
156 'variables' => serialize($log_entry['variables']),
157 'severity' => $log_entry['severity'],
158 'link' => drupal_substr($log_entry['link'], 0, 255),
159 'location' => $log_entry['request_uri'],
160 'referer' => $log_entry['referer'],
161 'hostname' => drupal_substr($log_entry['ip'], 0, 128),
162 'timestamp' => $log_entry['timestamp'],
166 catch (Exception $e) {
167 // Exception is ignored so that watchdog does not break pages during the
168 // installation process or is not able to create the watchdog table during
174 * Implements hook_form_FORM_ID_alter() for system_logging_settings().
176 function dblog_form_system_logging_settings_alter(&$form, $form_state) {
177 $form['dblog_row_limit'] = array(
179 '#title' => t('Database log messages to keep'),
180 '#default_value' => variable_get('dblog_row_limit', 1000),
181 '#options' => array(0 => t('All')) + drupal_map_assoc(array(100, 1000, 10000, 100000, 1000000)),
182 '#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')))
184 $form['actions']['#weight'] = 1;
188 * Implements hook_theme().
190 function dblog_theme() {
192 'dblog_message' => array(
193 'variables' => array('event' => NULL, 'link' => FALSE),
194 'file' => 'dblog.admin.inc',