summaryrefslogtreecommitdiff
path: root/modules/syslog/syslog.module
blob: e4aa298f9b58ade21f0f2089f420a2f004e3d30e (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
<?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("Syslog logs events by sending messages to the logging facility of your web server's operating system. 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.") . '</p>';
      $output .= '<h2>' . t('UNIX, Linux & Mac OS X') . '</h2>';
      $output .= '<p>' . t('On UNIX, Linux and Mac OS X, the file /etc/syslog.conf defines this 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 <code>syslog.conf</code>, see the <a href="@syslog_conf"><code>syslog.conf</code> manual page</a>.', array('@syslog_conf' => url('http://www.rt.com/man/syslog.5.html'))) . '</p>';
      $output .= '<h2>' . t('Microsoft Windows') . '</h2>';
      $output .= '<p>' . t('On Microsoft Windows messages are always sent to the Event Log using the code <code>LOG_USER</code>.') . '</p>';
      $output .= '<p>' . t('For more information, see the <a href="@syslog">online handbook</a> and  and PHP\'s <a href="@php_openlog">openlog</a> and <a href="@php_syslog">syslog</a> functions.', array('@syslog' => 'http://drupal.org/handbook/modules/syslog', '@php_openlog' => url('http://www.php.net/manual/function.openlog.php'), '@php_syslog' => url('http://www.php.net/manual/function.syslog.php'))) . '</p>';

      return $output;
  }
}

/**
 * Implement hook_form_FORM_ID_alter().
 */
function syslog_form_system_logging_settings_alter(&$form, &$form_state) {
  if (defined('LOG_LOCAL0')) {
    $help = module_exists('help') ? ' ' . l(t('More information'), 'admin/help/syslog') . '.' : NULL;
    $form['syslog_facility'] = array(
      '#type'          => 'select',
      '#title'         => t('Syslog facility'),
      '#default_value' => variable_get('syslog_facility', LOG_LOCAL0),
      '#options'       => syslog_facility_list(),
      '#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,
     );
    $form['buttons']['#weight'] = 1;
  }
}

 /**
 * List all possible syslog facilities for UNIX/Linux.
 *
 * @return array
 */
function syslog_facility_list() {
  return array(
    LOG_LOCAL0 => 'LOG_LOCAL0',
    LOG_LOCAL1 => 'LOG_LOCAL1',
    LOG_LOCAL2 => 'LOG_LOCAL2',
    LOG_LOCAL3 => 'LOG_LOCAL3',
    LOG_LOCAL4 => 'LOG_LOCAL4',
    LOG_LOCAL5 => 'LOG_LOCAL5',
    LOG_LOCAL6 => 'LOG_LOCAL6',
    LOG_LOCAL7 => 'LOG_LOCAL7',
  );
}

/**
 * Implement hook_watchdog().
 */
function syslog_watchdog(array $log_entry) {
  $log_init = &drupal_static(__FUNCTION__, FALSE);

  if (!$log_init) {
    $log_init = TRUE;
    $default_facility = defined('LOG_LOCAL0') ? LOG_LOCAL0 : LOG_USER;
    openlog('drupal', LOG_NDELAY, variable_get('syslog_facility', $default_facility));
  }

  syslog($log_entry['severity'], theme('syslog_format', array('entry' => $log_entry)));
}

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

/**
 * Format a system log entry.
 *
 * @ingroup themeable
 */
function theme_syslog_format($variables) {
  $entry = $variables['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;
}