View source
<?php
namespace Drupal\config_selector;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Logger\RfcLogLevel;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
class TestLogger implements DebugLoggerInterface, LoggerInterface {
protected static $logs;
public function __construct() {
if (empty(static::$logs)) {
$this
->clear();
}
}
public function getLogs($level = FALSE) {
return FALSE === $level ? static::$logs : static::$logs[$level];
}
public function clear() {
static::$logs = [
'emergency' => [],
'alert' => [],
'critical' => [],
'error' => [],
'warning' => [],
'notice' => [],
'info' => [],
'debug' => [],
];
}
public function log($level, $message, array $context = []) {
static $map = [
RfcLogLevel::DEBUG => 'debug',
RfcLogLevel::INFO => 'info',
RfcLogLevel::NOTICE => 'notice',
RfcLogLevel::WARNING => 'warning',
RfcLogLevel::ERROR => 'error',
RfcLogLevel::CRITICAL => 'critical',
RfcLogLevel::ALERT => 'alert',
RfcLogLevel::EMERGENCY => 'emergency',
];
$level = isset($map[$level]) ? $map[$level] : $level;
static::$logs[$level][] = (string) new FormattableMarkup($message, $context);
}
public function emergency($message, array $context = []) {
$this
->log('emergency', $message, $context);
}
public function alert($message, array $context = []) {
$this
->log('alert', $message, $context);
}
public function critical($message, array $context = []) {
$this
->log('critical', $message, $context);
}
public function error($message, array $context = []) {
$this
->log('error', $message, $context);
}
public function warning($message, array $context = []) {
$this
->log('warning', $message, $context);
}
public function notice($message, array $context = []) {
$this
->log('notice', $message, $context);
}
public function info($message, array $context = []) {
$this
->log('info', $message, $context);
}
public function debug($message, array $context = []) {
$this
->log('debug', $message, $context);
}
public static function register(ContainerBuilder $container) {
$container
->register('config_selector.test_logger', __CLASS__)
->addTag('logger');
}
public function countErrors() {
return count(static::$logs['critical']) + count(static::$logs['error']) + count(static::$logs['emergency']);
}
}