You are here

jsonlog.module in JSONlog 8

Contains jsonlog.module.

File

jsonlog.module
View source
<?php

/**
 * @file
 * Contains jsonlog.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_help().
 *
 * @param string $route_name
 * @param RouteMatchInterface $route_match
 */
function jsonlog_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the jsonlog module.
    case 'help.page.jsonlog':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Logs all events picked up by Drupal\'s LoggerInterface to a JSON-formatted file.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Adds this module's setting fields to the system logging settings form.
 * Implements hook_form_FORM_ID_alter().
 *
 * @see _jsonlog_form_system_logging_settings_alter()
 *
 * @param array $form
 * @param FormStateInterface $form_state
 */
function jsonlog_form_system_logging_settings_alter(&$form, FormStateInterface $form_state) {
  \Drupal::moduleHandler()
    ->loadInclude('jsonlog', 'inc');
  _jsonlog_form_system_logging_settings_alter($form, $form_state);
}

/**
 * @param array $form
 * @param FormStateInterface $form_state
 */
function jsonlog_form_system_logging_settings_validate($form, FormStateInterface $form_state) {
  $values =& $form_state
    ->getValues();

  // Non-empty truncate must be non-negative integer.
  if ($values['jsonlog_truncate'] !== '' && ($value = trim($values['jsonlog_truncate'])) !== '') {
    if (!preg_match('/^\\d+$/', $value)) {
      $form_state
        ->setErrorByName('jsonlog_truncate', t('\'@field\' is not a non-negative integer.', [
        '@field' => t('Truncate events to'),
      ]));
    }
  }
}

/**
 * Custom submit handler for the system logging settings form.
 *
 * @param array $form
 * @param FormStateInterface $form_state
 */
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']),
    )));
  }
}

Functions

Namesort descending Description
jsonlog_form_system_logging_settings_alter Adds this module's setting fields to the system logging settings form. Implements hook_form_FORM_ID_alter().
jsonlog_form_system_logging_settings_submit Custom submit handler for the system logging settings form.
jsonlog_form_system_logging_settings_validate
jsonlog_help Implements hook_help().