Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Redirects logging messages to syslog.
*/
/**
Dries Buytaert
committed
* Implements hook_help().
Dries Buytaert
committed
*/
Gábor Hojtsy
committed
function syslog_help($path, $arg) {
switch ($path) {
Dries Buytaert
committed
case 'admin/help#syslog':
Dries Buytaert
committed
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
Jennifer Hodgdon
committed
$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 <a href='!syslog'>online documentation for the Syslog module</a>, as well as PHP's documentation pages for the <a href='!php_openlog'>openlog</a> and <a href='!php_syslog'>syslog</a> functions.", array('!syslog' => 'https://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>';
Dries Buytaert
committed
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Logging for UNIX, Linux, and Mac OS X') . '</dt>';
$output .= '<dd>' . t('On UNIX, Linux, and Mac OS X, you will find the configuration in the file <em>/etc/syslog.conf</em>, or in <em>/etc/rsyslog.conf</em> or in the directory <em>/etc/rsyslog.d</em>. These files define 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> or <em>rsyslog.conf</em>, see the <em>syslog.conf</em> or <em>rsyslog.conf</em> manual page on your command line.') . '</dd>';
$output .= '<dt>' . t('Logging for Microsoft Windows') . '</dt>';
$output .= '<dd>' . t('On Microsoft Windows, messages are always sent to the Event Log using the code <code>LOG_USER</code>.') . '</dd>';
Dries Buytaert
committed
$output .= '</dl>';
Gábor Hojtsy
committed
return $output;
Dries Buytaert
committed
}
}
/**
Dries Buytaert
committed
* Implements hook_form_FORM_ID_alter().
*/
function syslog_form_system_logging_settings_alter(&$form, &$form_state) {
$config = \Drupal::config('syslog.settings');
$help = \Drupal::moduleHandler()->moduleExists('help') ? ' ' . l(t('More information'), 'admin/help/syslog') . '.' : NULL;
Dries Buytaert
committed
$form['syslog_identity'] = array(
'#type' => 'textfield',
'#title' => t('Syslog identity'),
Angie Byron
committed
'#default_value' => $config->get('identity'),
Dries Buytaert
committed
'#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,
);
if (defined('LOG_LOCAL0')) {
$form['syslog_facility'] = array(
'#type' => 'select',
'#title' => t('Syslog facility'),
Angie Byron
committed
'#default_value' => $config->get('facility'),
'#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,
);
}
Dries Buytaert
committed
$form['syslog_format'] = array(
'#type' => 'textarea',
'#title' => t('Syslog format'),
Angie Byron
committed
'#default_value' => $config->get('format'),
Dries Buytaert
committed
'#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>'),
);
Angie Byron
committed
$form['#submit'][] = 'syslog_logging_settings_submit';
}
/**
* Form submission handler for system_logging_settings().
*
* @see syslog_form_system_logging_settings_alter()
*/
function syslog_logging_settings_submit($form, &$form_state) {
\Drupal::config('syslog.settings')
Angie Byron
committed
->set('identity', $form_state['values']['syslog_identity'])
->set('facility', $form_state['values']['syslog_facility'])
->set('format', $form_state['values']['syslog_format'])
->save();
Dries Buytaert
committed
}
* Lists all possible syslog facilities for UNIX/Linux.
*
* @return array
* An array of syslog facilities for UNIX/Linux.
Dries Buytaert
committed
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',
Dries Buytaert
committed
);
}
Dries Buytaert
committed
/**
Dries Buytaert
committed
* Implements hook_watchdog().
Dries Buytaert
committed
*/
function syslog_watchdog(array $log_entry) {
global $base_url;
$log_init = &drupal_static(__FUNCTION__, FALSE);
$config = \Drupal::config('syslog.settings');
Dries Buytaert
committed
if (!$log_init) {
$log_init = TRUE;
Angie Byron
committed
$facility = $config->get('facility');
if ($facility === '') {
$facility = defined('LOG_LOCAL0') ? LOG_LOCAL0 : LOG_USER;
}
openlog($config->get('identity'), LOG_NDELAY, $facility);
Dries Buytaert
committed
}
Angie Byron
committed
$message = strtr($config->get('format'), array(
'!base_url' => $base_url,
'!timestamp' => $log_entry['timestamp'],
'!type' => $log_entry['type'],
'!ip' => $log_entry['ip'],
'!request_uri' => $log_entry['request_uri'],
'!referer' => $log_entry['referer'],
catch
committed
'!uid' => $log_entry['uid'],
'!link' => strip_tags($log_entry['link']),
'!message' => strip_tags(!isset($log_entry['variables']) ? $log_entry['message'] : strtr($log_entry['message'], $log_entry['variables'])),
));
Dries Buytaert
committed
syslog($log_entry['severity'], $message);