Initial Drupal 11 with DDEV setup
This commit is contained in:
@ -0,0 +1,6 @@
|
||||
identity: drupal
|
||||
# The default facility setting depends on the operating system, and will be
|
||||
# overwritten during installation.
|
||||
# @see syslog_install().
|
||||
facility: 8
|
||||
format: '!base_url|!timestamp|!type|!ip|!request_uri|!referer|!uid|!link|!message'
|
||||
15
web/core/modules/syslog/config/schema/syslog.schema.yml
Normal file
15
web/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: integer
|
||||
label: 'Facility'
|
||||
format:
|
||||
type: string
|
||||
label: 'Format'
|
||||
17
web/core/modules/syslog/migrations/d6_syslog_settings.yml
Normal file
17
web/core/modules/syslog/migrations/d6_syslog_settings.yml
Normal file
@ -0,0 +1,17 @@
|
||||
id: d6_syslog_settings
|
||||
label: System log configuration
|
||||
migration_tags:
|
||||
- Drupal 6
|
||||
- Configuration
|
||||
source:
|
||||
plugin: variable
|
||||
variables:
|
||||
- syslog_identity
|
||||
- syslog_facility
|
||||
source_module: syslog
|
||||
process:
|
||||
identity: syslog_identity
|
||||
facility: syslog_facility
|
||||
destination:
|
||||
plugin: config
|
||||
config_name: syslog.settings
|
||||
19
web/core/modules/syslog/migrations/d7_syslog_settings.yml
Normal file
19
web/core/modules/syslog/migrations/d7_syslog_settings.yml
Normal file
@ -0,0 +1,19 @@
|
||||
id: d7_syslog_settings
|
||||
label: Syslog configuration
|
||||
migration_tags:
|
||||
- Drupal 7
|
||||
- Configuration
|
||||
source:
|
||||
plugin: variable
|
||||
variables:
|
||||
- syslog_facility
|
||||
- syslog_format
|
||||
- syslog_identity
|
||||
source_module: syslog
|
||||
process:
|
||||
facility: syslog_facility
|
||||
format: syslog_format
|
||||
identity: syslog_identity
|
||||
destination:
|
||||
plugin: config
|
||||
config_name: syslog.settings
|
||||
@ -0,0 +1,5 @@
|
||||
finished:
|
||||
6:
|
||||
syslog: syslog
|
||||
7:
|
||||
syslog: syslog
|
||||
73
web/core/modules/syslog/src/Hook/SyslogHooks.php
Normal file
73
web/core/modules/syslog/src/Hook/SyslogHooks.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\syslog\Hook;
|
||||
|
||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\Core\Link;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\Core\Hook\Attribute\Hook;
|
||||
|
||||
/**
|
||||
* Hook implementations for syslog.
|
||||
*/
|
||||
class SyslogHooks {
|
||||
|
||||
use StringTranslationTrait;
|
||||
|
||||
/**
|
||||
* Implements hook_help().
|
||||
*/
|
||||
#[Hook('help')]
|
||||
public function help($route_name, RouteMatchInterface $route_match): ?string {
|
||||
switch ($route_name) {
|
||||
case 'help.page.syslog':
|
||||
$output = '';
|
||||
$output .= '<h2>' . $this->t('About') . '</h2>';
|
||||
$output .= '<p>' . $this->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>';
|
||||
$output .= '<h2>' . $this->t('Uses') . '</h2>';
|
||||
$output .= '<dl>';
|
||||
$output .= '<dt>' . $this->t('Logging for UNIX, Linux, and Mac OS X') . '</dt>';
|
||||
$output .= '<dd>' . $this->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>' . $this->t('Logging for Microsoft Windows') . '</dt>';
|
||||
$output .= '<dd>' . $this->t('On Microsoft Windows, messages are always sent to the Event Log using the code <code>LOG_USER</code>.') . '</dd>';
|
||||
$output .= '</dl>';
|
||||
return $output;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_form_FORM_ID_alter().
|
||||
*/
|
||||
#[Hook('form_system_logging_settings_alter')]
|
||||
public function formSystemLoggingSettingsAlter(&$form, FormStateInterface $form_state) : void {
|
||||
$config = \Drupal::configFactory()->getEditable('syslog.settings');
|
||||
$help = \Drupal::moduleHandler()->moduleExists('help') ? ' ' . Link::fromTextAndUrl($this->t('More information'), Url::fromRoute('help.page', ['name' => 'syslog']))->toString() . '.' : NULL;
|
||||
$form['syslog_identity'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Syslog identity'),
|
||||
'#default_value' => $config->get('identity'),
|
||||
'#description' => $this->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'] = [
|
||||
'#type' => 'select',
|
||||
'#title' => $this->t('Syslog facility'),
|
||||
'#default_value' => $config->get('facility'),
|
||||
'#options' => syslog_facility_list(),
|
||||
'#description' => $this->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'] = [
|
||||
'#type' => 'textarea',
|
||||
'#title' => $this->t('Syslog format'),
|
||||
'#default_value' => $config->get('format'),
|
||||
'#required' => TRUE,
|
||||
'#description' => $this->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>'),
|
||||
];
|
||||
$form['#submit'][] = 'syslog_logging_settings_submit';
|
||||
}
|
||||
|
||||
}
|
||||
119
web/core/modules/syslog/src/Logger/SysLog.php
Normal file
119
web/core/modules/syslog/src/Logger/SysLog.php
Normal file
@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\syslog\Logger;
|
||||
|
||||
use Drupal\Core\Config\ConfigFactoryInterface;
|
||||
use Drupal\Core\Logger\LogMessageParserInterface;
|
||||
use Drupal\Core\Logger\RfcLoggerTrait;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
// cspell:ignore ndelay
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
// Do not connect if identity or facility are not configured.
|
||||
$identity = $this->config->get('identity');
|
||||
$facility = $this->config->get('facility');
|
||||
if ($identity === NULL || $facility === NULL) {
|
||||
return;
|
||||
}
|
||||
$this->connectionOpened = openlog($identity, LOG_NDELAY, $facility);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function log($level, string|\Stringable $message, array $context = []): void {
|
||||
global $base_url;
|
||||
|
||||
$format = $this->config->get('format');
|
||||
// If no format is configured then a message will not be written to syslog
|
||||
// so return early. This occurs during installation of the syslog module
|
||||
// before configuration has been written.
|
||||
if (empty($format)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure we have a connection available.
|
||||
$this->openConnection();
|
||||
if (!$this->connectionOpened) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 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($format, [
|
||||
'!base_url' => $base_url,
|
||||
'!timestamp' => $context['timestamp'],
|
||||
'!type' => $context['channel'],
|
||||
'!ip' => $context['ip'],
|
||||
'!request_uri' => $context['request_uri'],
|
||||
'!referer' => $context['referer'],
|
||||
'!severity' => $level,
|
||||
'!uid' => $context['uid'],
|
||||
'!link' => strip_tags($context['link']),
|
||||
'!message' => strip_tags($message),
|
||||
]);
|
||||
|
||||
$this->syslogWrapper($level, $entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* A syslog wrapper to make syslog functionality testable.
|
||||
*
|
||||
* @param int $level
|
||||
* The syslog priority.
|
||||
* @param string $entry
|
||||
* The message to send to syslog function.
|
||||
*/
|
||||
protected function syslogWrapper($level, $entry) {
|
||||
syslog($level, $entry);
|
||||
}
|
||||
|
||||
}
|
||||
6
web/core/modules/syslog/syslog.info.yml
Normal file
6
web/core/modules/syslog/syslog.info.yml
Normal file
@ -0,0 +1,6 @@
|
||||
name: Syslog
|
||||
type: module
|
||||
description: "Logs events to the web server's system log."
|
||||
package: Core
|
||||
version: VERSION
|
||||
configure: system.logging_settings
|
||||
22
web/core/modules/syslog/syslog.install
Normal file
22
web/core/modules/syslog/syslog.install
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Install, update and uninstall functions for the syslog module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_install().
|
||||
*/
|
||||
function syslog_install(): void {
|
||||
// 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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_update_last_removed().
|
||||
*/
|
||||
function syslog_update_last_removed(): int {
|
||||
return 8400;
|
||||
}
|
||||
39
web/core/modules/syslog/syslog.module
Normal file
39
web/core/modules/syslog/syslog.module
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
*/
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* Form submission handler for system_logging_settings().
|
||||
*
|
||||
* @see syslog_form_system_logging_settings_alter()
|
||||
*/
|
||||
function syslog_logging_settings_submit($form, FormStateInterface $form_state): void {
|
||||
\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 [
|
||||
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',
|
||||
];
|
||||
}
|
||||
11
web/core/modules/syslog/syslog.services.yml
Normal file
11
web/core/modules/syslog/syslog.services.yml
Normal file
@ -0,0 +1,11 @@
|
||||
parameters:
|
||||
syslog.skip_procedural_hook_scan: true
|
||||
|
||||
services:
|
||||
_defaults:
|
||||
autoconfigure: true
|
||||
logger.syslog:
|
||||
class: Drupal\syslog\Logger\SysLog
|
||||
arguments: ['@config.factory', '@logger.log_message_parser']
|
||||
tags:
|
||||
- { name: logger }
|
||||
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\syslog_test\Logger;
|
||||
|
||||
use Drupal\syslog\Logger\SysLog;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Redirects logging messages to error_log.
|
||||
*/
|
||||
class SysLogTest extends SysLog implements LoggerInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function syslogWrapper($level, $entry) {
|
||||
$log_path = \Drupal::service('file_system')->realpath('public://syslog.log');
|
||||
error_log($entry . PHP_EOL, 3, $log_path);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
name: 'Syslog test'
|
||||
type: module
|
||||
description: 'Provides a test logger for syslog module.'
|
||||
package: Testing
|
||||
version: VERSION
|
||||
dependencies:
|
||||
- drupal:syslog
|
||||
@ -0,0 +1,7 @@
|
||||
services:
|
||||
logger.syslog_test:
|
||||
parent: logger.syslog
|
||||
class: Drupal\syslog_test\Logger\SysLogTest
|
||||
arguments: ['@config.factory', '@logger.log_message_parser']
|
||||
tags:
|
||||
- { name: logger }
|
||||
14
web/core/modules/syslog/tests/src/Functional/GenericTest.php
Normal file
14
web/core/modules/syslog/tests/src/Functional/GenericTest.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\syslog\Functional;
|
||||
|
||||
use Drupal\Tests\system\Functional\Module\GenericModuleTestBase;
|
||||
|
||||
/**
|
||||
* Generic module test for syslog.
|
||||
*
|
||||
* @group syslog
|
||||
*/
|
||||
class GenericTest extends GenericModuleTestBase {}
|
||||
46
web/core/modules/syslog/tests/src/Functional/SyslogTest.php
Normal file
46
web/core/modules/syslog/tests/src/Functional/SyslogTest.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\syslog\Functional;
|
||||
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
|
||||
/**
|
||||
* Tests syslog settings.
|
||||
*
|
||||
* @group syslog
|
||||
*/
|
||||
class SyslogTest extends BrowserTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = ['syslog'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $defaultTheme = 'stark';
|
||||
|
||||
/**
|
||||
* Tests the syslog settings page.
|
||||
*/
|
||||
public function testSettings(): void {
|
||||
$admin_user = $this->drupalCreateUser(['administer site configuration']);
|
||||
$this->drupalLogin($admin_user);
|
||||
|
||||
// If we're on Windows, there is no configuration form.
|
||||
if (defined('LOG_LOCAL6')) {
|
||||
$this->drupalGet('admin/config/development/logging');
|
||||
$this->submitForm(['syslog_facility' => LOG_LOCAL6], 'Save configuration');
|
||||
$this->assertSession()->pageTextContains('The configuration options have been saved.');
|
||||
|
||||
$this->drupalGet('admin/config/development/logging');
|
||||
// Should be one field.
|
||||
$field = $this->assertSession()->elementExists('xpath', '//option[@value="' . LOG_LOCAL6 . '"]');
|
||||
$this->assertSame('selected', $field->getAttribute('selected'), 'Facility value saved.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\syslog\Kernel\Migrate\d6;
|
||||
|
||||
use Drupal\Tests\SchemaCheckTestTrait;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
|
||||
|
||||
/**
|
||||
* Upgrade variables to syslog.settings.yml.
|
||||
*
|
||||
* @group migrate_drupal_6
|
||||
*/
|
||||
class MigrateSyslogConfigsTest extends MigrateDrupal6TestBase {
|
||||
|
||||
use SchemaCheckTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = ['syslog'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
// Enable syslog in the source database so that requirements are met.
|
||||
$this->sourceDatabase->update('system')
|
||||
->condition('name', 'syslog')
|
||||
->fields(['status' => '1'])
|
||||
->execute();
|
||||
$this->executeMigration('d6_syslog_settings');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests migration of syslog variables to syslog.settings.yml.
|
||||
*/
|
||||
public function testSyslogSettings(): void {
|
||||
$config = $this->config('syslog.settings');
|
||||
$this->assertSame('drupal', $config->get('identity'));
|
||||
$this->assertSame(128, $config->get('facility'));
|
||||
$this->assertConfigSchema(\Drupal::service('config.typed'), 'syslog.settings', $config->get());
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\syslog\Kernel\Migrate\d7;
|
||||
|
||||
use Drupal\Tests\SchemaCheckTestTrait;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
|
||||
|
||||
/**
|
||||
* Upgrade variables to syslog.settings.yml.
|
||||
*
|
||||
* @group syslog
|
||||
*/
|
||||
class MigrateSyslogConfigsTest extends MigrateDrupal7TestBase {
|
||||
|
||||
use SchemaCheckTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = ['syslog'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->installConfig(static::$modules);
|
||||
$this->executeMigration('d7_syslog_settings');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests migration of syslog variables to syslog.settings.yml.
|
||||
*/
|
||||
public function testSyslogSettings(): void {
|
||||
$config = $this->config('syslog.settings');
|
||||
// 8 == LOG_USER
|
||||
$this->assertSame(8, $config->get('facility'));
|
||||
$this->assertSame('!base_url|!timestamp|!type|!ip|!request_uri|!referer|!uid|!link|!message', $config->get('format'));
|
||||
$this->assertSame('drupal', $config->get('identity'));
|
||||
}
|
||||
|
||||
}
|
||||
109
web/core/modules/syslog/tests/src/Kernel/SyslogTest.php
Normal file
109
web/core/modules/syslog/tests/src/Kernel/SyslogTest.php
Normal file
@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\syslog\Kernel;
|
||||
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||
|
||||
/**
|
||||
* Test syslog logger functionality.
|
||||
*
|
||||
* @group syslog
|
||||
* @coversDefaultClass \Drupal\syslog\Logger\SysLog
|
||||
*/
|
||||
class SyslogTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = ['syslog', 'syslog_test'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->installConfig(['syslog']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::log
|
||||
*/
|
||||
public function testSyslogWriting(): void {
|
||||
|
||||
$request = Request::create('/page-not-found', 'GET', [], [], [], ['REMOTE_ADDR' => '1.2.3.4']);
|
||||
$request->headers->set('Referer', 'other-site');
|
||||
$request->setSession(new Session(new MockArraySessionStorage()));
|
||||
\Drupal::requestStack()->push($request);
|
||||
|
||||
$user = $this->getMockBuilder('Drupal\Core\Session\AccountInterface')->getMock();
|
||||
$user->method('id')->willReturn(42);
|
||||
$this->container->set('current_user', $user);
|
||||
|
||||
\Drupal::logger('my_module')->warning('My warning message.', ['link' => '/my-link']);
|
||||
|
||||
$log_filename = $this->container->get('file_system')->realpath('public://syslog.log');
|
||||
$logs = explode(PHP_EOL, file_get_contents($log_filename));
|
||||
$log = explode('|', $logs[0]);
|
||||
|
||||
global $base_url;
|
||||
$this->assertEquals($base_url, $log[0]);
|
||||
$this->assertEquals('my_module', $log[2]);
|
||||
$this->assertEquals('1.2.3.4', $log[3]);
|
||||
$this->assertEquals($base_url . '/page-not-found', $log[4]);
|
||||
$this->assertEquals('other-site', $log[5]);
|
||||
$this->assertEquals('42', $log[6]);
|
||||
$this->assertEquals('/my-link', $log[7]);
|
||||
$this->assertEquals('My warning message.', $log[8]);
|
||||
|
||||
// Test that an empty format prevents writing to the syslog.
|
||||
/** @var \Drupal\Core\Config\Config $config */
|
||||
$config = $this->container->get('config.factory')->getEditable('syslog.settings');
|
||||
$config->set('format', '');
|
||||
$config->save();
|
||||
unlink($log_filename);
|
||||
\Drupal::logger('my_module')->warning('My warning message.', ['link' => '/my-link']);
|
||||
$this->assertFileDoesNotExist($log_filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that missing facility prevents writing to the syslog.
|
||||
*
|
||||
* @covers ::openConnection
|
||||
*/
|
||||
public function testSyslogMissingFacility(): void {
|
||||
$config = $this->container->get('config.factory')->getEditable('syslog.settings');
|
||||
$config->clear('facility');
|
||||
$config->save();
|
||||
\Drupal::logger('my_module')->warning('My warning message.');
|
||||
$log_filename = $this->container->get('file_system')->realpath('public://syslog.log');
|
||||
$this->assertFileDoesNotExist($log_filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests severity level logging.
|
||||
*
|
||||
* @covers ::log
|
||||
*/
|
||||
public function testSyslogSeverity(): void {
|
||||
/** @var \Drupal\Core\Config\Config $config */
|
||||
$config = $this->container->get('config.factory')->getEditable('syslog.settings');
|
||||
$config->set('format', '!type|!message|!severity');
|
||||
$config->save();
|
||||
|
||||
\Drupal::logger('my_module')->warning('My warning message.');
|
||||
|
||||
$log_filename = $this->container->get('file_system')->realpath('public://syslog.log');
|
||||
$logs = explode(PHP_EOL, file_get_contents($log_filename));
|
||||
$log = explode('|', $logs[0]);
|
||||
|
||||
$this->assertEquals('my_module', $log[0]);
|
||||
$this->assertEquals('My warning message.', $log[1]);
|
||||
$this->assertEquals('4', $log[2]);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user