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

Dries's avatar
 
Dries committed
4
function watchdog_help($section = "admin/help#watchdog") {
Dries's avatar
 
Dries committed
5
  $output = "";
Dries's avatar
 
Dries committed
6 7

  switch ($section) {
Dries's avatar
 
Dries committed
8
    case 'admin/help#watchdog':
Dries's avatar
 
Dries committed
9 10
      $output .= t("
      <p>Watchdog module monitors your web site, capturing system events in a log to be reviewed by an authorized individual at a later time.  The watchdog log is simply a list of recorded events containing usage data, performance data, errors, warnings and operational information.  It is vital to <a href=\"%watchdog\">check the watchdog report</a> on a regular basis as it is often the only way to tell what is going on.</p>
Dries's avatar
 
Dries committed
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
      <p>To ease administration, the watchdog will automatically discard old log entries, <a href=\"%log-entry\">as configured</a>. Needs \"cron.php\" to discard the entries.</p>", array("%watchdog" => url("admin/logs"), "%log-entry" => url("admin/system/modules/watchdog")));
      break;
    case 'admin/logs':
      $output = t("The watchdog module monitors your web site, captures system events in a log and records them to be reviewed by an authorized individual at a later time.  The watchdog log is simply a list of events recorded during operation and contains usage data, performance data, errors, warnings and operational information. It is vital to check the watchdog report on a regular basis as it is often the only way to tell what is going on.");
      break;
    case 'admin/logs/user':
       $output = t("Watchdog events that have to do with users.");
       break;
    case 'admin/logs/regular':
      $output = t("Watchdog events that are \"normal\" and have no other classification.");
      break;
    case 'admin/logs/httpd':
      $output = t("Watchdog events that are from the web server.");
      break;
    case 'admin/logs/special':
      $output = t("Watchdog events about adding, changing, and moderating nodes and comments.");
      break;
    case 'admin/logs/search':
      $output = t("Watchdog events showing what users searched for.");
      break;
    case 'admin/logs/error':
      $output = t("Watchdog events about PHP and database errors.");
      break;
    case 'admin/logs/warning':
      $output = t("Watchdog warning events. These events don't stop Drupal from running, but are things you should know.");
      break;
    case 'admin/system/modules#description':
      $output = t("Logs and records system events.");
      break;
    case 'admin/system/modules/watchdog':
      $output = t("Watchdog logs your system events.  To see these events go to the <a href=\"%watchdog\">logs</a>. Since these logs can grow out of control if kept around forever, below set how long an item should be kept in the log.  Note that to discard entries as set below you must run \"cron.php\" regularly.", array("%watchdog" => url("admin/logs")));
Dries's avatar
 
Dries committed
42 43
      break;
  }
Dries's avatar
 
Dries committed
44

Dries's avatar
 
Dries committed
45
  return $output;
46 47
}

Dries's avatar
 
Dries committed
48
function watchdog_perm() {
Dries's avatar
 
Dries committed
49
  return array("administer watchdog");
Dries's avatar
 
Dries committed
50 51
}

Dries's avatar
 
Dries committed
52
function watchdog_link($type) {
Dries's avatar
 
Dries committed
53 54
  if ($type == "system") {
    if (user_access("administer watchdog")) {
Dries's avatar
 
Dries committed
55 56
      menu("admin/logs", t("logs"), "watchdog_admin", 7);
      menu("admin/logs/view", t("view details"), "watchdog_admin", 0, MENU_HIDE, MENU_LOCKED);
57
      foreach (_watchdog_get_message_types() as $type) {
Dries's avatar
 
Dries committed
58
        menu("admin/logs/$type", t($type), "watchdog_admin");
Dries's avatar
 
Dries committed
59
      }
Dries's avatar
 
Dries committed
60
    }
Dries's avatar
 
Dries committed
61 62 63
  }
}

64
function watchdog_settings() {
Dries's avatar
 
Dries committed
65 66
  $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200), "format_interval");
  $period[1000000000] = t('Never');
Dries's avatar
 
Dries committed
67
  $output .= form_select(t("Discard entries older than"), "watchdog_clear", variable_get("watchdog_clear", 604800), $period, t("The time watchdog entries should be kept.  Older entries will be automatically discarded.  Requires crontab."));
Dries's avatar
 
Dries committed
68 69 70
  return $output;
}

Dries's avatar
 
Dries committed
71
function watchdog_cron() {
Dries's avatar
 
Dries committed
72
  db_query("DELETE FROM {watchdog} WHERE ". time() ." - timestamp > ". variable_get("watchdog_clear", 604800));
Dries's avatar
 
Dries committed
73
}
Dries's avatar
 
Dries committed
74

Dries's avatar
 
Dries committed
75
function watchdog_overview($type) {
Dries's avatar
 
Dries committed
76 77 78 79
  foreach (_watchdog_get_message_types() as $key) {
    $query[$key] = "WHERE type = '". check_query($key) ."'";
  }
  $query['actions'] = "WHERE link != ''";
Dries's avatar
 
Dries committed
80

Dries's avatar
 
Dries committed
81 82
  $header = array(
    array("data" => t("date"), "field" => "w.timestamp", "sort" => "desc"),
Dries's avatar
 
Dries committed
83
    array("data" => t("message"), "field" => "w.message"),
Dries's avatar
 
Dries committed
84 85 86
    array("data" => t("user"), "field" => "u.name"),
    array("data" => t("operations"), "colspan" => "2")
  );
Dries's avatar
 
Dries committed
87
  $sql = "SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid ". ($type ? $query[$type] : "");
Dries's avatar
 
Dries committed
88 89
  $sql .= tablesort_sql($header);
  $result = pager_query($sql, 50);
Dries's avatar
 
Dries committed
90

Dries's avatar
 
Dries committed
91
  while ($watchdog = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
92 93
    $rows[] = array(
      array("data" => format_date($watchdog->timestamp, "small"), "class" => "watchdog-$watchdog->type"),
94
      array("data" => truncate_utf8(strip_tags($watchdog->message), 64), "class" => "watchdog-$watchdog->type"),
Dries's avatar
 
Dries committed
95 96
      array("data" => format_name($watchdog), "class" => "watchdog-$watchdog->type"),
      array("data" => $watchdog->link, "class" => "watchdog-$watchdog->type"),
Dries's avatar
 
Dries committed
97
      array("data" => l(t("view details"), "admin/logs/view/$watchdog->wid"), "class" => "watchdog-$watchdog->type")
Dries's avatar
 
Dries committed
98
    );
Dries's avatar
 
Dries committed
99
  }
Dries's avatar
 
Dries committed
100

Dries's avatar
 
Dries committed
101
  if (!$rows) {
Dries's avatar
 
Dries committed
102
    $rows[] = array(array("data" => t("No log messages available."), "colspan" => "5"));
Dries's avatar
 
Dries committed
103
  }
Dries's avatar
 
Dries committed
104

Dries's avatar
Dries committed
105
  $pager = theme("pager", NULL, 50, 0, tablesort_pager());
Dries's avatar
 
Dries committed
106 107 108
  if (!empty($pager)) {
    $rows[] = array(array("data" => $pager, "colspan" => "5"));
  }
Dries's avatar
 
Dries committed
109
  return theme("table", $header, $rows);
Dries's avatar
 
Dries committed
110 111 112
}

function watchdog_view($id) {
Dries's avatar
 
Dries committed
113

Dries's avatar
 
Dries committed
114
  $result = db_query("SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid WHERE w.wid = %d", $id);
Dries's avatar
 
Dries committed
115 116

  if ($watchdog = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
117
    $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">";
Dries's avatar
 
Dries committed
118 119 120 121 122 123
    $output .= " <tr><th>". t("Type") ."</th><td>$watchdog->type</td></tr>";
    $output .= " <tr><th>". t("Date") ."</th><td>". format_date($watchdog->timestamp, "large") ."</td></tr>";
    $output .= " <tr><th>". t("User") ."</th><td>". format_name($watchdog) ."</td></tr>";
    $output .= " <tr><th>". t("Location") ."</th><td>$watchdog->location</td></tr>";
    $output .= " <tr><th>". t("Message") ."</th><td>$watchdog->message</td></tr>";
    $output .= " <tr><th>". t("Hostname") ."</th><td>$watchdog->hostname</td></tr>";
Dries's avatar
 
Dries committed
124
    $output .= "</table>";
Dries's avatar
 
Dries committed
125

Dries's avatar
 
Dries committed
126 127
    return $output;
  }
Dries's avatar
 
Dries committed
128 129
}

Dries's avatar
 
Dries committed
130
function watchdog_admin() {
Dries's avatar
 
Dries committed
131

Dries's avatar
 
Dries committed
132 133 134 135 136 137 138 139 140
  switch (arg(2)) {
    case "help":
      $output = watchdog_help();
      break;
    case "view":
      $output = watchdog_view(arg(3));
      break;
    default:
      $output = watchdog_overview(arg(2));
Dries's avatar
 
Dries committed
141
  }
Dries's avatar
 
Dries committed
142
  print theme("page", $output);
Dries's avatar
 
Dries committed
143
}
Dries's avatar
 
Dries committed
144

Dries's avatar
 
Dries committed
145 146 147 148 149 150 151 152 153 154 155
function _watchdog_get_message_types() {
  $types = array();

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

  return $types;
}

Dries's avatar
 
Dries committed
156
?>