dblog.module 6.06 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
2
// $Id$
Dries's avatar
 
Dries committed
3

Dries's avatar
 
Dries committed
4 5 6 7
/**
 * @file
 * System monitoring and logging for administrators.
 *
8
 * The dblog module monitors your site and keeps a list of
Dries's avatar
 
Dries committed
9 10 11
 * recorded events containing usage and performance data, errors,
 * warnings, and similar operational information.
 *
12
 * @see watchdog()
Dries's avatar
 
Dries committed
13 14
 */

Dries's avatar
Dries committed
15
/**
16
 * Implements hook_help().
Dries's avatar
Dries committed
17
 */
18 19
function dblog_help($path, $arg) {
  switch ($path) {
20
    case 'admin/help#dblog':
21 22 23 24 25 26 27 28 29 30 31
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $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/handbook/modules/dblog')) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Monitoring your site') . '</dt>';
      $output .= '<dd>' . t('The Database logging module allows you to view an event log on the <a href="@dblog">Recent log entries</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>';
      $output .= '<dt>' . t('Debugging site problems') . '</dt>';
      $output .= '<dd>' . t('In case of errors or problems with the site, the <a href="@dblog">Recent log entries</a> page can be useful for debugging, since it shows the sequence of events. The log entries include usage information, warnings, and errors.', array('@dblog' => url('admin/reports/dblog'))) . '</dd>';
      $output .= '</dl>';
      return $output;	
32
    case 'admin/reports/dblog':
33
      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 entries report on a regular basis, as it is often the only way to tell what is going on.') . '</p>';
Dries's avatar
 
Dries committed
34
  }
35 36
}

Dries's avatar
 
Dries committed
37
/**
38
 * Implements hook_menu().
Dries's avatar
 
Dries committed
39
 */
40
function dblog_menu() {
41
  $items['admin/reports/dblog'] = array(
42 43
    'title' => 'Recent log entries',
    'description' => 'View events that have recently been logged.',
44
    'page callback' => 'dblog_overview',
45
    'access arguments' => array('access site reports'),
46
    'weight' => -1,
47
    'file' => 'dblog.admin.inc',
48
  );
49
  $items['admin/reports/page-not-found'] = array(
50 51
    'title' => "Top 'page not found' errors",
    'description' => "View 'page not found' errors (404s).",
52
    'page callback' => 'dblog_top',
53
    'page arguments' => array('page not found'),
54
    'access arguments' => array('access site reports'),
55
    'file' => 'dblog.admin.inc',
56
  );
57
  $items['admin/reports/access-denied'] = array(
58
    'title' => "Top 'access denied' errors",
59
    'description' => "View 'access denied' errors (403s).",
60
    'page callback' => 'dblog_top',
61
    'page arguments' => array('access denied'),
62
    'access arguments' => array('access site reports'),
63
    'file' => 'dblog.admin.inc',
64
  );
65
  $items['admin/reports/event/%'] = array(
66
    'title' => 'Details',
67
    'page callback' => 'dblog_event',
68
    'page arguments' => array(3),
69
    'access arguments' => array('access site reports'),
70
    'type' => MENU_CALLBACK,
71
    'file' => 'dblog.admin.inc',
72 73 74
  );
  return $items;
}
Dries's avatar
 
Dries committed
75

76
function dblog_init() {
77
  if (arg(0) == 'admin' && arg(1) == 'reports') {
78
    // Add the CSS for this module
79
    drupal_add_css(drupal_get_path('module', 'dblog') . '/dblog.css', array('preprocess' => FALSE));
80
  }
Dries's avatar
 
Dries committed
81 82
}

83 84


Dries's avatar
Dries committed
85
/**
86
 * Implements hook_cron().
Dries's avatar
Dries committed
87
 *
Dries's avatar
 
Dries committed
88
 * Remove expired log messages and flood control events.
Dries's avatar
Dries committed
89
 */
90 91
function dblog_cron() {
  // Cleanup the watchdog table
92 93 94 95 96 97
  if (variable_get('dblog_row_limit', 1000) > 0) {
    $max = db_query('SELECT MAX(wid) FROM {watchdog}')->fetchField();
    db_delete('watchdog')
      ->condition('wid', $max - variable_get('dblog_row_limit', 1000), '<=')
      ->execute();
  }
Dries's avatar
 
Dries committed
98
}
Dries's avatar
 
Dries committed
99

100
/**
101
 * Implements hook_user_cancel().
102
 */
103
function dblog_user_cancel($edit, $account, $method) {
104 105
  switch ($method) {
    case 'user_cancel_reassign':
106 107 108 109
      db_update('watchdog')
        ->fields(array('uid' => 0))
        ->condition('uid', $account->uid)
        ->execute();
110 111 112
      break;

    case 'user_cancel_delete':
113 114 115
      db_delete('watchdog')
        ->condition('uid', $account->uid)
        ->execute();
116 117
      break;
  }
118 119
}

120
function _dblog_get_message_types() {
Dries's avatar
 
Dries committed
121 122
  $types = array();

123
  $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
124
  foreach ($result as $object) {
Dries's avatar
 
Dries committed
125 126 127 128 129
    $types[] = $object->type;
  }

  return $types;
}
130

131
/**
132
 * Implements hook_watchdog().
133 134 135 136
 *
 * Note some values may be truncated for database column size restrictions.
 */
function dblog_watchdog(array $log_entry) {
137
  Database::getConnection('default', 'default')->insert('watchdog')
138
    ->fields(array(
139 140 141 142 143 144 145 146 147 148
      'uid' => $log_entry['user']->uid,
      'type' => substr($log_entry['type'], 0, 64),
      'message' => $log_entry['message'],
      'variables' => serialize($log_entry['variables']),
      'severity' => $log_entry['severity'],
      'link' => substr($log_entry['link'], 0, 255),
      'location' => $log_entry['request_uri'],
      'referer' => $log_entry['referer'],
      'hostname' => substr($log_entry['ip'], 0, 128),
      'timestamp' => $log_entry['timestamp'],
149 150
    ))
    ->execute();
151
}
152

153
/**
154
 * Implements hook_form_FORM_ID_alter().
155 156 157 158 159 160
 */
function dblog_form_system_logging_settings_alter(&$form, $form_state) {
  $form['dblog_row_limit'] = array(
    '#type' => 'select',
    '#title' => t('Database log entries to keep'),
    '#default_value' => variable_get('dblog_row_limit', 1000),
161
    '#options' => array(0 => t('All')) + drupal_map_assoc(array(100, 1000, 10000, 100000, 1000000)),
162 163
    '#description' => t('The maximum number of entries to keep in the database log. Requires a <a href="@cron">cron maintenance task</a>.', array('@cron' => url('admin/reports/status')))
  );
164
  $form['actions']['#weight'] = 1;
165 166
}