syslog.module 3.84 KB
Newer Older
1
2
3
4
5
6
7
8
<?php
// $Id$

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

9
10
11
12
13
14
15
if (defined('LOG_LOCAL0')) {
  define('DEFAULT_SYSLOG_FACILITY', LOG_LOCAL0);
}
else {
  define('DEFAULT_SYSLOG_FACILITY', LOG_USER);
}

16
17
18
19
20
21
22
23
24
25
26
27
28
/**
 * Implementation of hook_help().
 */
function syslog_help($section) {
  switch ($section) {
    case 'admin/help#syslog':
      return '<p>'. t('Provides the facility to log Drupal messages to the operating systems\' syslog facility.') .'</p>';
  }
}

function syslog_menu() {
  $items['admin/settings/logging/syslog'] = array(
    'title'          => t('Syslog'),
29
    'description'    => t('Settings for syslog logging. Syslog is a system administration logging tool, where messages are routed by facility and severity. It is more suitable for medium to large sites, and would not be suitable for shared hosting environments.'),
30
31
32
33
34
35
36
    'page callback'  => 'drupal_get_form',
    'page arguments' => array('syslog_admin_settings'),
  );
  return $items;
}

function syslog_admin_settings() {
37
  $form['syslog_facility'] = array(
38
    '#type'          => 'select',
39
    '#title'         => t('Syslog facility to send events to'),
40
    '#default_value' => variable_get('syslog_facility', DEFAULT_SYSLOG_FACILITY),
41
    '#options'       => syslog_facility_list(),
42
    '#description'   => t('Select the syslog facility to send Drupal\'s messages to. Syslog is a system administration logging tool, where messages are routed by facility and severity. It is more suitable for medium to large sites, and would not be suitable for shared hosting environments. In the file /etc/syslog.conf you define where messages go for any combination of facility and severity. For UNIX/Linux systems, Drupal can use the facilities user, local0 to local7, for Windows, you can only use the user facility. For more information on syslog facilities, severity levels, and how to setup a syslog.conf files, see !syslog_conf and !php', array(
43
44
45
46
47
48
49
50
      '!php'         => l("PHP's syslog", 'http://www.php.net/manual/en/function.openlog.php'),
      '!syslog_conf' => l('UNIX/Linux syslog.conf', 'http://www.rt.com/man/syslog.5.html'),
      )),
  );
  return system_settings_form($form);
}

function syslog_facility_list() {
51
  $facility_list = array(
52
53
    LOG_USER   => t('LOG_USER - User level messages. Use this for Windows.'),
  );
54
55
56
57
58
59
60
61
62
63
64
65
66
  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;
67
68
69
70
71
72
73
}

function syslog_watchdog($entry) {
  static $log_init = FALSE;

  if (!$log_init) {
    $log_init = TRUE;
74
    openlog('drupal', LOG_NDELAY, variable_get('syslog_facility', DEFAULT_SYSLOG_FACILITY));
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
  }

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

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

function theme_syslog_format($entry) {
  global $base_url;

  $message = t('@base_url|@timestamp|@type|@uid|@ip|@request_uri|@referer_uri|@link|@message',
    array(
      '@base_url'    => $base_url,
      '@timestamp'   => $entry['timestamp'],
      '@type'        => $entry['type'],
      '@ip'          => $entry['ip'],
      '@request_uri' => $entry['request_uri'],
      '@referer_uri' => $entry['referer'],
      '@uid'         => $entry['user']->uid,
      '@link'        => strip_tags($entry['link']),
101
102
      // Keep message English, but replace variable components, if present.
      '@message'     => strip_tags(is_null($entry['variables']) ? $entry['message'] : strtr($entry['message'], $entry['variables'])),
103
104
105
    ));
  return $message;
}