You are here

function jsonlog_form_system_logging_settings_submit in JSONlog 8

Same name and namespace in other branches
  1. 8.2 jsonlog.module \jsonlog_form_system_logging_settings_submit()
  2. 7.2 jsonlog.module \jsonlog_form_system_logging_settings_submit()
  3. 7 jsonlog.module \jsonlog_form_system_logging_settings_submit()
  4. 3.x jsonlog.module \jsonlog_form_system_logging_settings_submit()

Custom submit handler for the system logging settings form.

Parameters

array $form:

FormStateInterface $form_state:

1 string reference to 'jsonlog_form_system_logging_settings_submit'
_jsonlog_form_system_logging_settings_alter in ./jsonlog.inc
Adds this module's setting fields to the system logging settings form.

File

./jsonlog.module, line 68
Contains jsonlog.module.

Code

function jsonlog_form_system_logging_settings_submit($form, FormStateInterface $form_state) {
  \Drupal::moduleHandler()
    ->loadInclude('jsonlog', 'inc');
  $values =& $form_state
    ->getValues();
  $fields = [
    'jsonlog_severity_threshold',
    'jsonlog_truncate',
    'jsonlog_siteid',
    'jsonlog_dir',
    'jsonlog_file_time',
  ];
  foreach ($fields as $name) {

    // Trim all values.
    $values[$name] = trim($values[$name]);
  }
  if (!$values['jsonlog_truncate']) {
    $values['jsonlog_truncate'] = 0;
  }
  if (!$values['jsonlog_dir']) {
    $values['jsonlog_dir'] = jsonlog_default_dir();
  }

  // Tags.
  if (($values['jsonlog_tags'] = $v = trim($values['jsonlog_tags'])) !== '') {
    $v = str_replace([
      "\r",
      "\n",
    ], '', $v);
    $v = trim($v, ',');
    $v = preg_replace('/ *, */', ',', $v);
    $values['jsonlog_tags'] = trim($v);
  }
  \Drupal::configFactory()
    ->getEditable('jsonlog.settings')
    ->set('jsonlog_severity_threshold', $values['jsonlog_severity_threshold'])
    ->set('jsonlog_truncate', $values['jsonlog_truncate'])
    ->set('jsonlog_siteid', $values['jsonlog_siteid'])
    ->set('jsonlog_canonical', $values['jsonlog_canonical'])
    ->set('jsonlog_file_time', $values['jsonlog_file_time'])
    ->set('jsonlog_dir', $values['jsonlog_dir'])
    ->set('jsonlog_newline_prepend', (bool) $values['jsonlog_newline_prepend'])
    ->set('jsonlog_tags', $values['jsonlog_tags'])
    ->save();

  // Write a test-entry via our logger service with data from the example table
  if ($values['test_entry'] === 1) {

    /** @var \Drupal\jsonlog\Logger\JsonLog $jsonlogger */
    $jsonlogger = \Drupal::service('logger.jsonlog');
    $context = [
      'user' => \Drupal::currentUser()
        ->getAccount(),
      'ip' => $values['example_entry_data']['client_ip'],
      'request_uri' => $values['example_entry_data']['request_uri'],
      'channel' => $values['example_entry_data']['subtype'],
      'link' => $values['example_entry_data']['link'],
      'referer' => '',
    ];
    $jsonlogger
      ->log($values['jsonlog_severity_threshold'], $values['example_entry_data']['message'], $context);
    \Drupal::messenger()
      ->addMessage(t('An attempt was made to write a test entry to the json log at @file.', array(
      '@file' => $jsonlogger
        ->getFileName($values['jsonlog_file_time']),
    )));
  }
}