From 4a2894eea4aa1657129be6123f926117535d12c7 Mon Sep 17 00:00:00 2001 From: catch Date: Thu, 5 Aug 2021 09:05:33 +0100 Subject: [PATCH] Issue #3224414 by alexpott, varshith, dagmar, daffie: Installing the syslog module uses its configuration before it is written (cherry picked from commit 58a4457b910edba29d088303394ef378d5f4fb25) --- core/modules/syslog/src/Logger/SysLog.php | 10 +++++++++- core/modules/syslog/syslog.module | 1 + core/modules/syslog/tests/src/Kernel/SyslogTest.php | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/core/modules/syslog/src/Logger/SysLog.php b/core/modules/syslog/src/Logger/SysLog.php index 616e5ab20e4..7526841a192 100644 --- a/core/modules/syslog/src/Logger/SysLog.php +++ b/core/modules/syslog/src/Logger/SysLog.php @@ -63,6 +63,14 @@ class SysLog implements LoggerInterface { public function log($level, $message, array $context = []) { 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(); @@ -70,7 +78,7 @@ class SysLog implements LoggerInterface { $message_placeholders = $this->parser->parseMessagePlaceholders($message, $context); $message = empty($message_placeholders) ? $message : strtr($message, $message_placeholders); - $entry = strtr($this->config->get('format'), [ + $entry = strtr($format, [ '!base_url' => $base_url, '!timestamp' => $context['timestamp'], '!type' => $context['channel'], diff --git a/core/modules/syslog/syslog.module b/core/modules/syslog/syslog.module index c40bf33bbb6..28d57f7b056 100644 --- a/core/modules/syslog/syslog.module +++ b/core/modules/syslog/syslog.module @@ -55,6 +55,7 @@ function syslog_form_system_logging_settings_alter(&$form, FormStateInterface $f '#type' => 'textarea', '#title' => t('Syslog format'), '#default_value' => $config->get('format'), + '#required' => TRUE, '#description' => t('Specify the format of the syslog entry. Available variables are:
!base_url
Base URL of the site.
!timestamp
Unix timestamp of the log entry.
!type
The category to which this message belongs.
!ip
IP address of the user triggering the message.
!request_uri
The requested URI.
!referer
HTTP Referer if available.
!severity
The severity level of the event; ranges from 0 (Emergency) to 7 (Debug).
!uid
User ID.
!link
A link to associate with the message.
!message
The message to store in the log.
'), ]; diff --git a/core/modules/syslog/tests/src/Kernel/SyslogTest.php b/core/modules/syslog/tests/src/Kernel/SyslogTest.php index 306602ed653..ceb1845c9cb 100644 --- a/core/modules/syslog/tests/src/Kernel/SyslogTest.php +++ b/core/modules/syslog/tests/src/Kernel/SyslogTest.php @@ -51,6 +51,15 @@ class SyslogTest extends KernelTestBase { $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); } /**