syslog.module 5.24 KB
Newer Older
1 2 3 4 5 6 7
<?php

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

8
use Drupal\Core\Url;
9
use Drupal\Core\Form\FormStateInterface;
10
use Drupal\Core\Routing\RouteMatchInterface;
11

12
/**
13
 * Implements hook_help().
14
 */
15
function syslog_help($route_name, RouteMatchInterface $route_match) {
16 17
  switch ($route_name) {
    case 'help.page.syslog':
18 19
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
20
      $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="http://php.net/manual/function.openlog.php">openlog</a> and <a href="http://php.net/manual/function.syslog.php">syslog</a> functions.', [':syslog' => 'https://www.drupal.org/documentation/modules/syslog']) . '</p>';
21 22
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
23
      $output .= '<dt>' . t('Logging for UNIX, Linux, and Mac OS X') . '</dt>';
24
      $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>';
25 26
      $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>';
27
      $output .= '</dl>';
28
      return $output;
29 30 31
  }
}

32
/**
33
 * Implements hook_form_FORM_ID_alter().
34
 */
35
function syslog_form_system_logging_settings_alter(&$form, FormStateInterface $form_state) {
36
  $config = \Drupal::configFactory()->getEditable('syslog.settings');
37
  $help = \Drupal::moduleHandler()->moduleExists('help') ? ' ' . \Drupal::l(t('More information'), new Url('help.page', ['name' => 'syslog'])) . '.' : NULL;
38
  $form['syslog_identity'] = [
39 40
    '#type'          => 'textfield',
    '#title'         => t('Syslog identity'),
41
    '#default_value' => $config->get('identity'),
42
    '#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,
43
  ];
44
  if (defined('LOG_LOCAL0')) {
45
    $form['syslog_facility'] = [
46 47
      '#type'          => 'select',
      '#title'         => t('Syslog facility'),
48
      '#default_value' => $config->get('facility'),
49 50
      '#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,
51
     ];
52
  }
53
  $form['syslog_format'] = [
54 55
    '#type'          => 'textarea',
    '#title'         => t('Syslog format'),
56
    '#default_value' => $config->get('format'),
57
    '#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>!severity</code></dt><dd>The severity level of the event; ranges from 0 (Emergency) to 7 (Debug).</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>'),
58
  ];
59 60 61 62 63 64 65 66 67

  $form['#submit'][] = 'syslog_logging_settings_submit';
}

/**
 * Form submission handler for system_logging_settings().
 *
 * @see syslog_form_system_logging_settings_alter()
 */
68
function syslog_logging_settings_submit($form, FormStateInterface $form_state) {
69
  \Drupal::configFactory()->getEditable('syslog.settings')
70 71 72
    ->set('identity', $form_state->getValue('syslog_identity'))
    ->set('facility', $form_state->getValue('syslog_facility'))
    ->set('format', $form_state->getValue('syslog_format'))
73
    ->save();
74 75
}

76
/**
77
 * Lists all possible syslog facilities for UNIX/Linux.
78 79
 *
 * @return array
80
 *   An array of syslog facilities for UNIX/Linux.
81
 */
82
function syslog_facility_list() {
83
  return [
84 85 86 87 88 89 90 91
    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',
92
  ];
93
}