Commit 4a2894ee authored by catch's avatar catch
Browse files

Issue #3224414 by alexpott, varshith, dagmar, daffie: Installing the syslog...

Issue #3224414 by alexpott, varshith, dagmar, daffie: Installing the syslog module uses its configuration before it is written

(cherry picked from commit 58a4457b)
parent c23e67cc
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -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'],
+1 −0
Original line number Diff line number Diff line
@@ -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>'),
  ];

+9 −0
Original line number Diff line number Diff line
@@ -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);
  }

  /**