Commit 9ae6dbd4 authored by catch's avatar catch
Browse files

Issue #3333215 by enchufe, arunkumark, mfb, Nitin shrivastava, smustgrave,...

Issue #3333215 by enchufe, arunkumark, mfb, Nitin shrivastava, smustgrave, cilefen: Return early if syslog configs are NULL to avoid openlog deprecation

(cherry picked from commit 5ce13375)
parent 5e775aae
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -52,8 +52,13 @@ public function __construct(ConfigFactoryInterface $config_factory, LogMessagePa
   */
  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 @@ public function log($level, string|\Stringable $message, array $context = []): v

    // 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);
+14 −0
Original line number Diff line number Diff line
@@ -62,6 +62,20 @@ public function testSyslogWriting() {
    $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.
   *