Projet

Général

Profil

Paste
Télécharger (7,18 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / dblog / dblog.module @ 01dfd3b5

1
<?php
2

    
3
/**
4
 * @file
5
 * System monitoring and logging for administrators.
6
 *
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.
10
 *
11
 * @see watchdog()
12
 */
13

    
14
/**
15
 * Implements hook_help().
16
 */
17
function dblog_help($path, $arg) {
18
  switch ($path) {
19
    case 'admin/help#dblog':
20
      $output = '';
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>';
24
      $output .= '<dl>';
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>';
29
      $output .= '</dl>';
30
      return $output;
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>';
33
  }
34
}
35

    
36
/**
37
 * Implements hook_menu().
38
 */
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'),
45
    'weight' => -1,
46
    'file' => 'dblog.admin.inc',
47
  );
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',
55
  );
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',
63
  );
64
  $items['admin/reports/event/%'] = array(
65
    'title' => 'Details',
66
    'page callback' => 'dblog_event',
67
    'page arguments' => array(3),
68
    'access arguments' => array('access site reports'),
69
    'file' => 'dblog.admin.inc',
70
  );
71

    
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',
80
    );
81
  }
82

    
83
  return $items;
84
}
85

    
86
/**
87
 * Implements hook_init().
88
 */
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');
93
  }
94
}
95

    
96
/**
97
 * Implements hook_cron().
98
 *
99
 * Controls the size of the log table, paring it to 'dblog_row_limit' messages.
100
 */
101
function dblog_cron() {
102
  // Cleanup the watchdog table.
103
  $row_limit = variable_get('dblog_row_limit', 1000);
104

    
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();
114

    
115
    // Delete all table entries older than the nth row, if nth row was found.
116
    if ($min_row) {
117
      db_delete('watchdog')
118
        ->condition('wid', $min_row, '<')
119
        ->execute();
120
    }
121
  }
122
}
123

    
124
/**
125
 * Gathers a list of uniquely defined database log message types.
126
 *
127
 * @return array
128
 *   List of uniquely defined database log message types.
129
 */
130
function _dblog_get_message_types() {
131
  $types = array();
132

    
133
  $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
134
  foreach ($result as $object) {
135
    $types[] = $object->type;
136
  }
137

    
138
  return $types;
139
}
140

    
141
/**
142
 * Implements hook_watchdog().
143
 *
144
 * Note: Some values may be truncated to meet database column size restrictions.
145
 */
146
function dblog_watchdog(array $log_entry) {
147
  if (!function_exists('drupal_substr')) {
148
    require_once DRUPAL_ROOT . '/includes/unicode.inc';
149
  }
150
  try {
151
    Database::getConnection('default', 'default')->insert('watchdog')
152
      ->fields(array(
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'],
163
      ))
164
      ->execute();
165
  }
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
169
    // installation.
170
  }
171
}
172

    
173
/**
174
 * Implements hook_form_FORM_ID_alter() for system_logging_settings().
175
 */
176
function dblog_form_system_logging_settings_alter(&$form, $form_state) {
177
  $form['dblog_row_limit'] = array(
178
    '#type' => 'select',
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')))
183
  );
184
  $form['actions']['#weight'] = 1;
185
}
186

    
187
/**
188
 * Implements hook_theme().
189
 */
190
function dblog_theme() {
191
  return array(
192
    'dblog_message' => array(
193
      'variables' => array('event' => NULL, 'link' => FALSE),
194
      'file' => 'dblog.admin.inc',
195
    ),
196
  );
197
}