You are here

StateLogger.php in Preview Link 2.0.x

Same filename and directory in other branches
  1. 2.x tests/modules/preview_link_test/src/StateLogger.php

File

tests/modules/preview_link_test/src/StateLogger.php
View source
<?php

namespace Drupal\preview_link_test;

use Drupal\Core\Logger\LogMessageParser;
use Drupal\Core\State\StateInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;

/**
 * Sends logs to state.
 */
final class StateLogger implements LoggerInterface {
  use LoggerTrait;
  const STATE_LOGGER = 'state_preview_link_test_logs';

  /**
   * State.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * StateLogger constructor.
   */
  public function __construct(StateInterface $state) {
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public function log($level, $message, array $context = []) {
    $logs = $this
      ->getLogs();
    $messagePlaceholders = (new LogMessageParser())
      ->parseMessagePlaceholders($message, $context);
    $logs[] = [
      $level,
      $message,
      $messagePlaceholders,
      // Whitelist context keys to prevent serializing container, etc.
      array_intersect_key($context, array_flip([
        'channel',
      ])),
    ];
    $this->state
      ->set(static::STATE_LOGGER, $logs);
  }

  /**
   * Gets the captured logs.
   *
   * @return array
   *   An array of logs, which contain the arguments passed to log().
   */
  public function getLogs() : array {
    return $this->state
      ->get(static::STATE_LOGGER, []);
  }

  /**
   * Deletes captured logs.
   */
  public function cleanLogs() : void {
    $this->state
      ->delete(static::STATE_LOGGER);
  }

}

Classes

Namesort descending Description
StateLogger Sends logs to state.