You are here

class TestLogger in Configuration selector 8.2

Same name and namespace in other branches
  1. 8 src/TestLogger.php \Drupal\config_selector\TestLogger

A test logger.

Hierarchy

  • class \Drupal\config_selector\TestLogger implements \Symfony\Component\HttpKernel\Log\DebugLoggerInterface, \Psr\Log\LoggerInterface

Expanded class hierarchy of TestLogger

1 file declares its use of TestLogger
ConfigSelectorTest.php in tests/src/Kernel/ConfigSelectorTest.php

File

src/TestLogger.php, line 14

Namespace

Drupal\config_selector
View source
class TestLogger implements DebugLoggerInterface, LoggerInterface {

  /**
   * An array of log messages keyed by type.
   *
   * @var array
   */
  protected static $logs;

  /**
   * TestLogger constructor.
   */
  public function __construct() {
    if (empty(static::$logs)) {
      $this
        ->clear();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getLogs($level = FALSE) {
    return FALSE === $level ? static::$logs : static::$logs[$level];
  }

  /**
   * {@inheritdoc}
   */
  public function clear() {
    static::$logs = [
      'emergency' => [],
      'alert' => [],
      'critical' => [],
      'error' => [],
      'warning' => [],
      'notice' => [],
      'info' => [],
      'debug' => [],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function log($level, $message, array $context = []) {

    // Convert levels...
    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);
  }

  /**
   * {@inheritdoc}
   */
  public function emergency($message, array $context = []) {
    $this
      ->log('emergency', $message, $context);
  }

  /**
   * {@inheritdoc}
   */
  public function alert($message, array $context = []) {
    $this
      ->log('alert', $message, $context);
  }

  /**
   * {@inheritdoc}
   */
  public function critical($message, array $context = []) {
    $this
      ->log('critical', $message, $context);
  }

  /**
   * {@inheritdoc}
   */
  public function error($message, array $context = []) {
    $this
      ->log('error', $message, $context);
  }

  /**
   * {@inheritdoc}
   */
  public function warning($message, array $context = []) {
    $this
      ->log('warning', $message, $context);
  }

  /**
   * {@inheritdoc}
   */
  public function notice($message, array $context = []) {
    $this
      ->log('notice', $message, $context);
  }

  /**
   * {@inheritdoc}
   */
  public function info($message, array $context = []) {
    $this
      ->log('info', $message, $context);
  }

  /**
   * {@inheritdoc}
   */
  public function debug($message, array $context = []) {
    $this
      ->log('debug', $message, $context);
  }

  /**
   * Registers the test logger to the container.
   *
   * @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
   *   The ContainerBuilder to register the test logger to.
   */
  public static function register(ContainerBuilder $container) {
    $container
      ->register('config_selector.test_logger', __CLASS__)
      ->addTag('logger');
  }

  /**
   * {@inheritdoc}
   */
  public function countErrors() {
    return count(static::$logs['critical']) + count(static::$logs['error']) + count(static::$logs['emergency']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TestLogger::$logs protected static property An array of log messages keyed by type.
TestLogger::alert public function Action must be taken immediately.
TestLogger::clear public function
TestLogger::countErrors public function Returns the number of errors.
TestLogger::critical public function Critical conditions.
TestLogger::debug public function Detailed debug information.
TestLogger::emergency public function System is unusable.
TestLogger::error public function Runtime errors that do not require immediate action but should typically be logged and monitored.
TestLogger::getLogs public function Returns an array of logs.
TestLogger::info public function Interesting events.
TestLogger::log public function Logs with an arbitrary level.
TestLogger::notice public function Normal but significant events.
TestLogger::register public static function Registers the test logger to the container.
TestLogger::warning public function Exceptional occurrences that are not errors.
TestLogger::__construct public function TestLogger constructor.