Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
3
core/modules/syslog/config/install/syslog.settings.yml
Normal file
3
core/modules/syslog/config/install/syslog.settings.yml
Normal file
|
@ -0,0 +1,3 @@
|
|||
identity: drupal
|
||||
facility: ''
|
||||
format: '!base_url|!timestamp|!type|!ip|!request_uri|!referer|!uid|!link|!message'
|
15
core/modules/syslog/config/schema/syslog.schema.yml
Normal file
15
core/modules/syslog/config/schema/syslog.schema.yml
Normal file
|
@ -0,0 +1,15 @@
|
|||
# Schema for the configuration files of the syslog module.
|
||||
|
||||
syslog.settings:
|
||||
type: config_object
|
||||
label: 'Syslog settings'
|
||||
mapping:
|
||||
identity:
|
||||
type: string
|
||||
label: 'Identity'
|
||||
facility:
|
||||
type: string
|
||||
label: 'Facility'
|
||||
format:
|
||||
type: string
|
||||
label: 'Format'
|
96
core/modules/syslog/src/Logger/SysLog.php
Normal file
96
core/modules/syslog/src/Logger/SysLog.php
Normal file
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\syslog\Logger\SysLog.
|
||||
*/
|
||||
|
||||
namespace Drupal\syslog\Logger;
|
||||
|
||||
use Drupal\Core\Config\ConfigFactoryInterface;
|
||||
use Drupal\Core\Logger\LogMessageParserInterface;
|
||||
use Drupal\Core\Logger\RfcLoggerTrait;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Redirects logging messages to syslog.
|
||||
*/
|
||||
class SysLog implements LoggerInterface {
|
||||
use RfcLoggerTrait;
|
||||
|
||||
/**
|
||||
* A configuration object containing syslog settings.
|
||||
*
|
||||
* @var \Drupal\Core\Config\Config
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* The message's placeholders parser.
|
||||
*
|
||||
* @var \Drupal\Core\Logger\LogMessageParserInterface
|
||||
*/
|
||||
protected $parser;
|
||||
|
||||
/**
|
||||
* Stores whether there is a system logger connection opened or not.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $connectionOpened = FALSE;
|
||||
|
||||
/**
|
||||
* Constructs a SysLog object.
|
||||
*
|
||||
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
|
||||
* The configuration factory object.
|
||||
* @param \Drupal\Core\Logger\LogMessageParserInterface $parser
|
||||
* The parser to use when extracting message variables.
|
||||
*/
|
||||
public function __construct(ConfigFactoryInterface $config_factory, LogMessageParserInterface $parser) {
|
||||
$this->config = $config_factory->get('syslog.settings');
|
||||
$this->parser = $parser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a connection to the system logger.
|
||||
*/
|
||||
protected function openConnection() {
|
||||
if (!$this->connectionOpened) {
|
||||
$facility = $this->config->get('facility');
|
||||
if ($facility === '') {
|
||||
$facility = defined('LOG_LOCAL0') ? LOG_LOCAL0 : LOG_USER;
|
||||
}
|
||||
$this->connectionOpened = openlog($this->config->get('identity'), LOG_NDELAY, $facility);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function log($level, $message, array $context = array()) {
|
||||
global $base_url;
|
||||
|
||||
// Ensure we have a connection available.
|
||||
$this->openConnection();
|
||||
|
||||
// Populate the message placeholders and then replace them in the message.
|
||||
$message_placeholders = $this->parser->parseMessagePlaceholders($message, $context);
|
||||
$message = empty($message_placeholders) ? $message : strtr($message, $message_placeholders);
|
||||
|
||||
$entry = strtr($this->config->get('format'), array(
|
||||
'!base_url' => $base_url,
|
||||
'!timestamp' => $context['timestamp'],
|
||||
'!type' => $context['channel'],
|
||||
'!ip' => $context['ip'],
|
||||
'!request_uri' => $context['request_uri'],
|
||||
'!referer' => $context['referer'],
|
||||
'!uid' => $context['uid'],
|
||||
'!link' => strip_tags($context['link']),
|
||||
'!message' => strip_tags($message),
|
||||
));
|
||||
|
||||
syslog($level, $entry);
|
||||
}
|
||||
|
||||
}
|
45
core/modules/syslog/src/Tests/SyslogTest.php
Normal file
45
core/modules/syslog/src/Tests/SyslogTest.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\syslog\Tests\SyslogTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\syslog\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests syslog settings.
|
||||
*
|
||||
* @group syslog
|
||||
*/
|
||||
class SyslogTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('syslog');
|
||||
|
||||
/**
|
||||
* Tests the syslog settings page.
|
||||
*/
|
||||
function testSettings() {
|
||||
$admin_user = $this->drupalCreateUser(array('administer site configuration'));
|
||||
$this->drupalLogin($admin_user);
|
||||
|
||||
// If we're on Windows, there is no configuration form.
|
||||
if (defined('LOG_LOCAL6')) {
|
||||
$this->drupalPostForm('admin/config/development/logging', array('syslog_facility' => LOG_LOCAL6), t('Save configuration'));
|
||||
$this->assertText(t('The configuration options have been saved.'));
|
||||
|
||||
$this->drupalGet('admin/config/development/logging');
|
||||
if ($this->parse()) {
|
||||
$field = $this->xpath('//option[@value=:value]', array(':value' => LOG_LOCAL6)); // Should be one field.
|
||||
$this->assertTrue($field[0]['selected'] == 'selected', 'Facility value saved.');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
7
core/modules/syslog/syslog.info.yml
Normal file
7
core/modules/syslog/syslog.info.yml
Normal file
|
@ -0,0 +1,7 @@
|
|||
name: Syslog
|
||||
type: module
|
||||
description: 'Logs and records system events to syslog.'
|
||||
package: Core
|
||||
version: VERSION
|
||||
core: 8.x
|
||||
configure: system.logging_settings
|
15
core/modules/syslog/syslog.install
Normal file
15
core/modules/syslog/syslog.install
Normal file
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Install, update and uninstall functions for the syslog module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_install().
|
||||
*/
|
||||
function syslog_install() {
|
||||
// The default facility setting depends on the operating system, so it needs
|
||||
// to be set dynamically during installation.
|
||||
\Drupal::configFactory()->getEditable('syslog.settings')->set('facility', defined('LOG_LOCAL0') ? LOG_LOCAL0 : LOG_USER)->save();
|
||||
}
|
93
core/modules/syslog/syslog.module
Normal file
93
core/modules/syslog/syslog.module
Normal file
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Redirects logging messages to syslog.
|
||||
*/
|
||||
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
|
||||
/**
|
||||
* Implements hook_help().
|
||||
*/
|
||||
function syslog_help($route_name, RouteMatchInterface $route_match) {
|
||||
switch ($route_name) {
|
||||
case 'help.page.syslog':
|
||||
$output = '';
|
||||
$output .= '<h3>' . t('About') . '</h3>';
|
||||
$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://www.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>';
|
||||
$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>';
|
||||
$output .= '</dl>';
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_form_FORM_ID_alter().
|
||||
*/
|
||||
function syslog_form_system_logging_settings_alter(&$form, FormStateInterface $form_state) {
|
||||
$config = \Drupal::configFactory()->getEditable('syslog.settings');
|
||||
$help = \Drupal::moduleHandler()->moduleExists('help') ? ' ' . \Drupal::l(t('More information'), new Url('help.page', ['name' => 'syslog'])) . '.' : NULL;
|
||||
$form['syslog_identity'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Syslog identity'),
|
||||
'#default_value' => $config->get('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('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('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, FormStateInterface $form_state) {
|
||||
\Drupal::configFactory()->getEditable('syslog.settings')
|
||||
->set('identity', $form_state->getValue('syslog_identity'))
|
||||
->set('facility', $form_state->getValue('syslog_facility'))
|
||||
->set('format', $form_state->getValue('syslog_format'))
|
||||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists 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',
|
||||
);
|
||||
}
|
6
core/modules/syslog/syslog.services.yml
Normal file
6
core/modules/syslog/syslog.services.yml
Normal file
|
@ -0,0 +1,6 @@
|
|||
services:
|
||||
logger.syslog:
|
||||
class: Drupal\syslog\Logger\SysLog
|
||||
arguments: ['@config.factory', '@logger.log_message_parser']
|
||||
tags:
|
||||
- { name: logger }
|
Reference in a new issue