From 5ce13375caf7785a1d06036c4ac3f0df917c9df3 Mon Sep 17 00:00:00 2001 From: catch Date: Fri, 14 Jul 2023 08:00:13 +0100 Subject: [PATCH] Issue #3333215 by enchufe, arunkumark, mfb, Nitin shrivastava, smustgrave, cilefen: Return early if syslog configs are NULL to avoid openlog deprecation --- core/modules/syslog/src/Logger/SysLog.php | 10 +++++++++- .../modules/syslog/tests/src/Kernel/SyslogTest.php | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/core/modules/syslog/src/Logger/SysLog.php b/core/modules/syslog/src/Logger/SysLog.php index 5e1b4eded3c..93edfd917b1 100644 --- a/core/modules/syslog/src/Logger/SysLog.php +++ b/core/modules/syslog/src/Logger/SysLog.php @@ -52,8 +52,13 @@ class SysLog implements LoggerInterface { */ 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'); - $this->connectionOpened = openlog($this->config->get('identity'), LOG_NDELAY, $facility); + if ($identity === NULL || $facility === NULL) { + return; + } + $this->connectionOpened = openlog($identity, LOG_NDELAY, $facility); } } @@ -73,6 +78,9 @@ class SysLog implements LoggerInterface { // 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); diff --git a/core/modules/syslog/tests/src/Kernel/SyslogTest.php b/core/modules/syslog/tests/src/Kernel/SyslogTest.php index ceb1845c9cb..14137c73efa 100644 --- a/core/modules/syslog/tests/src/Kernel/SyslogTest.php +++ b/core/modules/syslog/tests/src/Kernel/SyslogTest.php @@ -62,6 +62,20 @@ class SyslogTest extends KernelTestBase { $this->assertFileDoesNotExist($log_filename); } + /** + * Tests that missing facility prevents writing to the syslog. + * + * @covers ::openConnection + */ + public function testSyslogMissingFacility() { + $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. *