Project

General

Profile

Paste
Download (5.87 KB) Statistics
| Branch: | Revision:

root / drupal7 / modules / syslog / syslog.module @ 27e02aed

1
<?php
2

    
3
/**
4
 * @file
5
 * Redirects logging messages to syslog.
6
 */
7

    
8
if (defined('LOG_LOCAL0')) {
9
  /**
10
   * Sets the proper logging facility.
11
   *
12
   * Note that LOG_LOCAL0 through LOG_LOCAL7 are not available on Windows, so we
13
   * check for availability. If LOG_LOCAL0 is defined by the PHP environment, we
14
   * set that as the default; if not, we use LOG_USER.
15
   *
16
   * @see http://php.net/manual/function.syslog.php
17
   */
18
  define('DEFAULT_SYSLOG_FACILITY', LOG_LOCAL0);
19
}
20
else {
21
  define('DEFAULT_SYSLOG_FACILITY', LOG_USER);
22
}
23

    
24
/**
25
 * Implements hook_help().
26
 */
27
function syslog_help($path, $arg) {
28
  switch ($path) {
29
    case 'admin/help#syslog':
30
      $output = '';
31
      $output .= '<h3>' . t('About') . '</h3>';
32
      $output .= '<p>' . t("The Syslog module logs events by sending messages to the logging facility of your web server's operating system. Syslog is an operating system administrative logging tool that 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. For more information, see the online handbook entry for <a href='@syslog'>Syslog module</a> and PHP's <a href='@php_openlog'>openlog</a> and <a href='@php_syslog'>syslog</a> functions.", array('@syslog' => 'http://drupal.org/documentation/modules/syslog', '@php_openlog' => 'http://www.php.net/manual/function.openlog.php', '@php_syslog' => 'http://www.php.net/manual/function.syslog.php')) . '</p>';
33
      $output .= '<h3>' . t('Uses') . '</h3>';
34
      $output .= '<dl>';
35
      $output .= '<dt>' . t('Logging for UNIX, Linux, and Mac OS X') . '</dt>';
36
      $output .= '<dd>' . t('On UNIX, Linux, and Mac OS X, the file <em>/etc/syslog.conf</em> defines the routing configuration. Messages can be flagged with the codes <code>LOG_LOCAL0</code> through <code>LOG_LOCAL7</code>. For information on Syslog facilities, severity levels, and how to set up <em>syslog.conf</em>, see the <em>syslog.conf</em> manual page on your command line.') . '</dd>';
37
      $output .= '<dt>' . t('Logging for Microsoft Windows') . '</dt>';
38
      $output .= '<dd>' . t('On Microsoft Windows, messages are always sent to the Event Log using the code <code>LOG_USER</code>.') . '</dd>';
39
      $output .= '</dl>';
40
      return $output;
41
  }
42
}
43

    
44
/**
45
 * Implements hook_form_FORM_ID_alter().
46
 */
47
function syslog_form_system_logging_settings_alter(&$form, &$form_state) {
48
  $help = module_exists('help') ? ' ' . l(t('More information'), 'admin/help/syslog') . '.' : NULL;
49
  $form['syslog_identity'] = array(
50
    '#type'          => 'textfield',
51
    '#title'         => t('Syslog identity'),
52
    '#default_value' => variable_get('syslog_identity', 'drupal'),
53
    '#description'   => t('A string that will be prepended to every message logged to Syslog. If you have multiple sites logging to the same Syslog log file, a unique identity per site makes it easy to tell the log entries apart.') . $help,
54
  );
55
  if (defined('LOG_LOCAL0')) {
56
    $form['syslog_facility'] = array(
57
      '#type'          => 'select',
58
      '#title'         => t('Syslog facility'),
59
      '#default_value' => variable_get('syslog_facility', LOG_LOCAL0),
60
      '#options'       => syslog_facility_list(),
61
      '#description'   => t('Depending on the system configuration, Syslog and other logging tools use this code to identify or filter messages from within the entire system log.') . $help,
62
     );
63
  }
64
  $form['syslog_format'] = array(
65
    '#type'          => 'textarea',
66
    '#title'         => t('Syslog format'),
67
    '#default_value' => variable_get('syslog_format', '!base_url|!timestamp|!type|!ip|!request_uri|!referer|!uid|!link|!message'),
68
    '#description'   => t('Specify the format of the syslog entry. Available variables are: <dl><dt><code>!base_url</code></dt><dd>Base URL of the site.</dd><dt><code>!timestamp</code></dt><dd>Unix timestamp of the log entry.</dd><dt><code>!type</code></dt><dd>The category to which this message belongs.</dd><dt><code>!ip</code></dt><dd>IP address of the user triggering the message.</dd><dt><code>!request_uri</code></dt><dd>The requested URI.</dd><dt><code>!referer</code></dt><dd>HTTP Referer if available.</dd><dt><code>!uid</code></dt><dd>User ID.</dd><dt><code>!link</code></dt><dd>A link to associate with the message.</dd><dt><code>!message</code></dt><dd>The message to store in the log.</dd></dl>'),
69
  );
70
  $form['actions']['#weight'] = 1;
71
}
72

    
73
/**
74
 * Lists all possible syslog facilities for UNIX/Linux.
75
 *
76
 * @return array
77
 *   An array of syslog facilities for UNIX/Linux.
78
 */
79
function syslog_facility_list() {
80
  return array(
81
    LOG_LOCAL0 => 'LOG_LOCAL0',
82
    LOG_LOCAL1 => 'LOG_LOCAL1',
83
    LOG_LOCAL2 => 'LOG_LOCAL2',
84
    LOG_LOCAL3 => 'LOG_LOCAL3',
85
    LOG_LOCAL4 => 'LOG_LOCAL4',
86
    LOG_LOCAL5 => 'LOG_LOCAL5',
87
    LOG_LOCAL6 => 'LOG_LOCAL6',
88
    LOG_LOCAL7 => 'LOG_LOCAL7',
89
  );
90
}
91

    
92
/**
93
 * Implements hook_watchdog().
94
 */
95
function syslog_watchdog(array $log_entry) {
96
  global $base_url;
97

    
98
  $log_init = &drupal_static(__FUNCTION__, FALSE);
99

    
100
  if (!$log_init) {
101
    $log_init = TRUE;
102
    $default_facility = defined('LOG_LOCAL0') ? LOG_LOCAL0 : LOG_USER;
103
    openlog(variable_get('syslog_identity', 'drupal'), LOG_NDELAY, variable_get('syslog_facility', $default_facility));
104
  }
105

    
106
  $message = strtr(variable_get('syslog_format', '!base_url|!timestamp|!type|!ip|!request_uri|!referer|!uid|!link|!message'), array(
107
    '!base_url'    => $base_url,
108
    '!timestamp'   => $log_entry['timestamp'],
109
    '!type'        => $log_entry['type'],
110
    '!ip'          => $log_entry['ip'],
111
    '!request_uri' => $log_entry['request_uri'],
112
    '!referer'     => $log_entry['referer'],
113
    '!uid'         => $log_entry['uid'],
114
    '!link'        => strip_tags($log_entry['link']),
115
    '!message'     => strip_tags(!isset($log_entry['variables']) ? $log_entry['message'] : strtr($log_entry['message'], $log_entry['variables'])),
116
  ));
117

    
118
  syslog($log_entry['severity'], $message);
119
}