summaryrefslogtreecommitdiffstats
path: root/modules/dblog/dblog.module
blob: 04529d64aefe09f47c16d6f258ea0b956a5745e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
// $Id$

/**
 * @file
 * System monitoring and logging for administrators.
 *
 * The dblog module monitors your site and keeps a list of
 * recorded events containing usage and performance data, errors,
 * warnings, and similar operational information.
 *
 * @see watchdog()
 */

/**
 * Implementation of hook_help().
 */
function dblog_help($path, $arg) {
  switch ($path) {
    case 'admin/help#dblog':
      $output = '<p>'. t('The dblog module monitors your system, capturing system events in a log to be reviewed by an authorized individual at a later time. This is useful for site administrators who want a quick overview of activities on their site. The logs also record the sequence of events, so it can be useful for debugging site errors.') .'</p>';
      $output .= '<p>'. t('The dblog log is simply a list of recorded events containing usage data, performance data, errors, warnings and operational information. Administrators should check the dblog report on a regular basis to ensure their site is working properly.') .'</p>';
      $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@dblog">Dblog module</a>.', array('@dblog' => 'http://drupal.org/handbook/modules/dblog/')) .'</p>';
      return $output;
    case 'admin/reports/dblog':
      return '<p>'. t('The dblog module monitors your website, capturing system events in a log to be reviewed by an authorized individual at a later time. The dblog log is simply a list of recorded events containing usage data, performance data, errors, warnings and operational information. It is vital to check the dblog report on a regular basis as it is often the only way to tell what is going on.') .'</p>';
  }
}

/**
 * Implementation of hook_theme()
 */
function dblog_theme() {
  return array(
    'dblog_filters' => array(
      'arguments' => array('form' => NULL),
    ),
  );
}

/**
 * Implementation of hook_menu().
 */
function dblog_menu() {
  $items['admin/settings/logging/dblog'] = array(
    'title' => 'Database logging',
    'description' => 'Settings for logging to the Drupal database logs. This is the most common method for small to medium sites on shared hosting. The logs are viewable from the admin pages.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('dblog_admin_settings'),
    'file' => 'dblog.admin.inc',
  );

  $items['admin/reports/dblog'] = array(
    'title' => 'Recent log entries',
    'description' => 'View events that have recently been logged.',
    'page callback' => 'dblog_overview',
    'weight' => -1,
    'file' => 'dblog.admin.inc',
  );
  $items['admin/reports/page-not-found'] = array(
    'title' => "Top 'page not found' errors",
    'description' => "View 'page not found' errors (404s).",
    'page callback' => 'dblog_top',
    'page arguments' => array('page not found'),
    'file' => 'dblog.admin.inc',
  );
  $items['admin/reports/access-denied'] = array(
    'title' => "Top 'access denied' errors",
    'description' => "View 'access denied' errors (403s).",
    'page callback' => 'dblog_top',
    'page arguments' => array('access denied'),
    'file' => 'dblog.admin.inc',
  );
  $items['admin/reports/event/%'] = array(
    'title' => 'Details',
    'page callback' => 'dblog_event',
    'page arguments' => array(3),
    'type' => MENU_CALLBACK,
    'file' => 'dblog.admin.inc',
  );
  return $items;
}

function dblog_init() {
  if (arg(0) == 'admin' && arg(1) == 'reports') {
    // Add the CSS for this module
    drupal_add_css(drupal_get_path('module', 'dblog') .'/dblog.css', 'module', 'all', FALSE);
  }
}



/**
 * Implementation of hook_cron().
 *
 * Remove expired log messages and flood control events.
 */
function dblog_cron() {
  // Cleanup the watchdog table
  $max = db_result(db_query('SELECT MAX(wid) FROM {watchdog}'));
  db_query('DELETE FROM {watchdog} WHERE wid <= %d', $max - variable_get('dblog_row_limit', 1000));
}

/**
 * Implementation of hook_user().
 */
function dblog_user($op, &$edit, &$user) {
  if ($op == 'delete') {
    db_query('UPDATE {watchdog} SET uid = 0 WHERE uid = %d', $user->uid);
  }
}

function _dblog_get_message_types() {
  $types = array();

  $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
  while ($object = db_fetch_object($result)) {
    $types[] = $object->type;
  }

  return $types;
}

function dblog_watchdog($log = array()) {
  $current_db = db_set_active();
  db_query("INSERT INTO {watchdog}
    (uid, type, message, variables, severity, link, location, referer, hostname, timestamp)
    VALUES
    (%d, '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', %d)",
    $log['user']->uid,
    $log['type'],
    $log['message'],
    serialize($log['variables']),
    $log['severity'],
    $log['link'],
    $log['request_uri'],
    $log['referer'],
    $log['ip'],
    $log['timestamp']);

  if ($current_db) {
    db_set_active($current_db);
  }
}

/**
 * Theme dblog administration filter selector.
 *
 * @ingroup themeable
 */
function theme_dblog_filters($form) {
  $output = '';
  foreach (element_children($form['status']) as $key) {
    $output .= drupal_render($form['status'][$key]);
  }
  $output .= '<div id="dblog-admin-buttons">'. drupal_render($form['buttons']) .'</div>';
  return $output;
}