StateLogger.php in Preview Link 2.0.x
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;
final class StateLogger implements LoggerInterface {
use LoggerTrait;
const STATE_LOGGER = 'state_preview_link_test_logs';
protected $state;
public function __construct(StateInterface $state) {
$this->state = $state;
}
public function log($level, $message, array $context = []) {
$logs = $this
->getLogs();
$messagePlaceholders = (new LogMessageParser())
->parseMessagePlaceholders($message, $context);
$logs[] = [
$level,
$message,
$messagePlaceholders,
array_intersect_key($context, array_flip([
'channel',
])),
];
$this->state
->set(static::STATE_LOGGER, $logs);
}
public function getLogs() : array {
return $this->state
->get(static::STATE_LOGGER, []);
}
public function cleanLogs() : void {
$this->state
->delete(static::STATE_LOGGER);
}
}