- <?php
-
- * @file
- * Redirects logging messages to syslog.
- */
-
- if (defined('LOG_LOCAL0')) {
-
- * Sets the proper logging facility.
- *
- * Note that LOG_LOCAL0 through LOG_LOCAL7 are not available on Windows, so we
- * check for availability. If LOG_LOCAL0 is defined by the PHP environment, we
- * set that as the default; if not, we use LOG_USER.
- *
- * @see http://php.net/manual/function.syslog.php
- */
- define('DEFAULT_SYSLOG_FACILITY', LOG_LOCAL0);
- }
- else {
- define('DEFAULT_SYSLOG_FACILITY', LOG_USER);
- }
-
- * Implements hook_form_FORM_ID_alter().
- */
- function syslog_form_system_logging_settings_alter(&$form, &$form_state) {
- $config = config('system.core');
- $help = module_exists('help') ? ' ' . l(t('More information'), 'admin/help/syslog') . '.' : NULL;
- $form['syslog_identity'] = array(
- '#type' => 'textfield',
- '#title' => t('Syslog identity'),
- '#default_value' => $config->get('log_identity'),
- '#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'),
- '#default_value' => $config->get('log_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,
- );
- }
- $form['syslog_format'] = array(
- '#type' => 'textarea',
- '#title' => t('Syslog format'),
- '#default_value' => $config->get('log_format'),
- '#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>'),
- );
- $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) {
- config('system.core')
- ->set('log_identity', $form_state['values']['syslog_identity'])
- ->set('log_facility', $form_state['values']['syslog_facility'])
- ->set('log_format', $form_state['values']['syslog_format'])
- ->save();
- }
-
- * List all possible syslog facilities for UNIX/Linux.
- *
- * @return array
- * An array of syslog facilities for UNIX/Linux.
- */
- 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',
- );
- }
-
- * Implements hook_watchdog().
- */
- function syslog_watchdog(array $log_entry) {
- global $base_url;
-
- $log_init = &backdrop_static(__FUNCTION__, FALSE);
- $config = config('system.core');
-
- if (!$log_init) {
- $log_init = TRUE;
- $facility = $config->get('log_facility');
- if ($facility === '') {
- $facility = defined('LOG_LOCAL0') ? LOG_LOCAL0 : LOG_USER;
- }
- openlog($config->get('log_identity'), LOG_NDELAY, $facility);
- }
-
- $message = strtr($config->get('log_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'],
- '!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'])),
- ));
-
- syslog($log_entry['severity'], $message);
- }