dblog.module 6.27 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
      $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>';
27
      $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>';
28
      $output .= '<dt>' . t('Debugging site problems') . '</dt>';
29
      $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>';
30
      $output .= '</dl>';
31
      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 messages 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
    'title' => 'Recent log messages',
43
    '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
    'file' => 'dblog.admin.inc',
71
  );
72 73 74 75 76 77 78 79 80 81 82 83

  if (module_exists('search')) {
    $items['admin/reports/search'] = array(
      'title' => 'Top search phrases',
      'description' => 'View most popular search phrases.',
      'page callback' => 'dblog_top',
      'page arguments' => array('search'),
      'access arguments' => array('access site reports'),
      'file' => 'dblog.admin.inc',
    );
  }

84 85
  return $items;
}
Dries's avatar
 
Dries committed
86

87 88 89
/**
 * Implements hook_init().
 */
90
function dblog_init() {
91
  if (arg(0) == 'admin' && arg(1) == 'reports') {
92
    // Add the CSS for this module
93
    drupal_add_css(drupal_get_path('module', 'dblog') . '/dblog.css');
94
  }
Dries's avatar
 
Dries committed
95 96
}

Dries's avatar
Dries committed
97
/**
98
 * Implements hook_cron().
Dries's avatar
Dries committed
99
 *
Dries's avatar
 
Dries committed
100
 * Remove expired log messages and flood control events.
Dries's avatar
Dries committed
101
 */
102 103
function dblog_cron() {
  // Cleanup the watchdog table
104
  if (variable_get('dblog_row_limit', 1000) > 0) {
105 106
    $max = Database::getConnection('default', 'default')->query('SELECT MAX(wid) FROM {watchdog}')->fetchField();
    Database::getConnection('default', 'default')->delete('watchdog')
107 108 109
      ->condition('wid', $max - variable_get('dblog_row_limit', 1000), '<=')
      ->execute();
  }
Dries's avatar
 
Dries committed
110
}
Dries's avatar
 
Dries committed
111

112
function _dblog_get_message_types() {
Dries's avatar
 
Dries committed
113 114
  $types = array();

115
  $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
116
  foreach ($result as $object) {
Dries's avatar
 
Dries committed
117 118 119 120 121
    $types[] = $object->type;
  }

  return $types;
}
122

123
/**
124
 * Implements hook_watchdog().
125 126 127 128
 *
 * Note some values may be truncated for database column size restrictions.
 */
function dblog_watchdog(array $log_entry) {
129
  Database::getConnection('default', 'default')->insert('watchdog')
130
    ->fields(array(
131 132 133 134 135 136 137 138 139 140
      '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'],
141 142
    ))
    ->execute();
143
}
144

145
/**
146
 * Implements hook_form_FORM_ID_alter().
147 148 149 150
 */
function dblog_form_system_logging_settings_alter(&$form, $form_state) {
  $form['dblog_row_limit'] = array(
    '#type' => 'select',
151
    '#title' => t('Database log messages to keep'),
152
    '#default_value' => variable_get('dblog_row_limit', 1000),
153
    '#options' => array(0 => t('All')) + drupal_map_assoc(array(100, 1000, 10000, 100000, 1000000)),
154
    '#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')))
155
  );
156
  $form['actions']['#weight'] = 1;
157
}
158 159 160 161 162 163 164 165 166 167 168 169

/**
 * Implements hook_theme().
 */
function dblog_theme() {
  return array(
    'dblog_message' => array(
      'variables' => array('event' => NULL, 'link' => FALSE),
      'file' => 'dblog.admin.inc',
    ),
  );
}