diff --git a/core/modules/syslog/src/Logger/SysLog.php b/core/modules/syslog/src/Logger/SysLog.php index 616e5ab20e404d413668be7697c415c28e234a4a..7526841a19275cea5c90f769c293d7064d373936 100644 --- a/core/modules/syslog/src/Logger/SysLog.php +++ b/core/modules/syslog/src/Logger/SysLog.php @@ -63,6 +63,14 @@ protected function openConnection() { 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 @@ public function log($level, $message, array $context = []) { $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 c40bf33bbb61064f7eb4c2e917c369fc5a2081cc..28d57f7b0565cea110f3d7894e233ea2c503ce2d 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: <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>'), ]; diff --git a/core/modules/syslog/tests/src/Kernel/SyslogTest.php b/core/modules/syslog/tests/src/Kernel/SyslogTest.php index 306602ed653ade51206089b628c4c1c865631002..ceb1845c9cb30dec63695e74908f224f4c0cd908 100644 --- a/core/modules/syslog/tests/src/Kernel/SyslogTest.php +++ b/core/modules/syslog/tests/src/Kernel/SyslogTest.php @@ -51,6 +51,15 @@ public function testSyslogWriting() { $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); } /**