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

(cherry picked from commit 58a4457b91)
merge-requests/47/merge
catch 2021-08-05 09:05:33 +01:00
parent c23e67cc61
commit 4a2894eea4
3 changed files with 19 additions and 1 deletions

View File

@ -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'],

View File

@ -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>'),
];

View File

@ -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);
}
/**