summaryrefslogtreecommitdiff
path: root/modules/syslog/syslog.module
blob: 5fc2e519b0a3bd75b3fcbe9570cad2fbd6a72660 (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
<?php
// $Id$

/**
 * @file
 * Redirects logging messages to syslog.
 */

if (defined('LOG_LOCAL0')) {
  define('DEFAULT_SYSLOG_FACILITY', LOG_LOCAL0);
}
else {
  define('DEFAULT_SYSLOG_FACILITY', LOG_USER);
}

/**
 * Implement hook_help().
 */
function syslog_help($path, $arg) {
  switch ($path) {
    case 'admin/help#syslog':
      $output = '<p>' . t("The syslog module enables Drupal to send messages to the operating system's logging facility.") . '</p>';
      $output .= '<p>' . t('Syslog is an operating system administrative logging tool, and provides valuable information for use in system management and security auditing. Most suited to medium and large sites, syslog provides filtering tools that allow messages to be routed by type and severity. On UNIX/Linux systems, the file /etc/syslog.conf defines this routing configuration; on Microsoft Windows, all messages are sent to the Event Log. For more information on syslog facilities, severity levels, and how to set up a syslog.conf file, see <a href="@syslog_conf">UNIX/Linux syslog.conf</a> and PHP\'s <a href="@php_openlog">openlog</a> and <a href="@php_syslog">syslog</a> functions.', array('@syslog_conf' => url('http://www.rt.com/man/syslog.5.html'), '@php_openlog' => url('http://www.php.net/manual/function.openlog.php'), '@php_syslog' => url('http://www.php.net/manual/function.syslog.php'))) . '</p>';
      $output .= '<p>' . t('For more information, see the online handbook entry for <a href="@syslog">Syslog module</a>.', array('@syslog' => 'http://drupal.org/handbook/modules/syslog')) . '</p>';
      return $output;
  }
}

/**
 * Implement hook_form_FORM_ID_alter().
 */
function syslog_form_system_logging_settings_alter(&$form, &$form_state) {
  $form['syslog_facility'] = array(
    '#type'          => 'select',
    '#title'         => t('Send events to this syslog facility'),
    '#default_value' => variable_get('syslog_facility', DEFAULT_SYSLOG_FACILITY),
    '#options'       => syslog_facility_list(),
    '#description'   => t('Select the syslog facility code under which Drupal\'s messages should be sent. On UNIX/Linux systems, Drupal can flag its messages with the code LOG_LOCAL0 through LOG_LOCAL7; for Microsoft Windows, all messages are flagged with the code LOG_USER. Depending on the system configuration, syslog and other logging tools use this code to identify or filter Drupal messages from within the entire system log. For more information on syslog, see <a href="@syslog_help">Syslog help</a>.', array(
      '@syslog_help' => url('admin/help/syslog'))),
  );
  $form['buttons']['#weight'] = 1;
}

function syslog_facility_list() {
  $facility_list = array(
    LOG_USER   => t('LOG_USER - User level messages. Use this for Windows.'),
  );
  if (defined('LOG_LOCAL0')) {
    $facility_list += array(
      LOG_LOCAL0 => t('LOG_LOCAL0 - Local 0'),
      LOG_LOCAL1 => t('LOG_LOCAL1 - Local 1'),
      LOG_LOCAL2 => t('LOG_LOCAL2 - Local 2'),
      LOG_LOCAL3 => t('LOG_LOCAL3 - Local 3'),
      LOG_LOCAL4 => t('LOG_LOCAL4 - Local 4'),
      LOG_LOCAL5 => t('LOG_LOCAL5 - Local 5'),
      LOG_LOCAL6 => t('LOG_LOCAL6 - Local 6'),
      LOG_LOCAL7 => t('LOG_LOCAL7 - Local 7'),
    );
  }
  return $facility_list;
}

/**
 * Implement hook_watchdog().
 */
function syslog_watchdog(array $log_entry) {
  static $log_init = FALSE;

  if (!$log_init) {
    $log_init = TRUE;
    openlog('drupal', LOG_NDELAY, variable_get('syslog_facility', DEFAULT_SYSLOG_FACILITY));
  }

  syslog($log_entry['severity'], theme('syslog_format', $log_entry));
}

function syslog_theme() {
  return array(
    'syslog_format' => array(
      'arguments' => array('entry' => NULL),
    ),
  );
}

/**
 * Format a system log entry.
 *
 * @ingroup themeable
 */
function theme_syslog_format($entry) {
  global $base_url;

  $message  = $base_url;
  $message .= '|' . $entry['timestamp'];
  $message .= '|' . $entry['type'];
  $message .= '|' . $entry['ip'];
  $message .= '|' . $entry['request_uri'];
  $message .= '|' . $entry['referer'];
  $message .= '|' . $entry['user']->uid;
  $message .= '|' . strip_tags($entry['link']);
  $message .= '|' . strip_tags(is_null($entry['variables']) ? $entry['message'] : strtr($entry['message'], $entry['variables']));

  return $message;
}