You are here

final class StateLogger in Preview Link 2.x

Same name and namespace in other branches
  1. 2.0.x tests/modules/preview_link_test/src/StateLogger.php \Drupal\preview_link_test\StateLogger

Sends logs to state.

Hierarchy

  • class \Drupal\preview_link_test\StateLogger implements \Psr\Log\LoggerInterface uses \Psr\Log\LoggerTrait

Expanded class hierarchy of StateLogger

1 string reference to 'StateLogger'
preview_link_test.services.yml in tests/modules/preview_link_test/preview_link_test.services.yml
tests/modules/preview_link_test/preview_link_test.services.yml
1 service uses StateLogger
logger.preview_link_test in tests/modules/preview_link_test/preview_link_test.services.yml
Drupal\preview_link_test\StateLogger

File

tests/modules/preview_link_test/src/StateLogger.php, line 13

Namespace

Drupal\preview_link_test
View source
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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StateLogger::$state protected property State.
StateLogger::cleanLogs public function Deletes captured logs.
StateLogger::getLogs public function Gets the captured logs.
StateLogger::log public function
StateLogger::STATE_LOGGER constant
StateLogger::__construct public function StateLogger constructor.