You are here

class LoggerChannelPart in Purge 8.3

Provides a subchannel whichs logs to a single main channel with permissions.

Hierarchy

Expanded class hierarchy of LoggerChannelPart

1 file declares its use of LoggerChannelPart
LoggerChannelPartTest.php in tests/src/Unit/Logger/LoggerChannelPartTest.php

File

src/Logger/LoggerChannelPart.php, line 12

Namespace

Drupal\purge\Logger
View source
class LoggerChannelPart implements LoggerChannelPartInterface {

  /**
   * Access levels for each RFC 5424 log type.
   *
   * The constructor changes the granted levels to TRUE so that $grants
   * doesn't have to be searched/iterated each and every time.
   *
   * @var bool[]
   */
  protected $access = [
    RfcLogLevel::EMERGENCY => FALSE,
    RfcLogLevel::ALERT => FALSE,
    RfcLogLevel::CRITICAL => FALSE,
    RfcLogLevel::ERROR => FALSE,
    RfcLogLevel::WARNING => FALSE,
    RfcLogLevel::NOTICE => FALSE,
    RfcLogLevel::INFO => FALSE,
    RfcLogLevel::DEBUG => FALSE,
  ];

  /**
   * The identifier of the channel part.
   *
   * @var string
   */
  protected $id = '';

  /**
   * Permitted RFC 5424 log types.
   *
   * @var int[]
   */
  protected $grants = [];

  /**
   * The single and central logger channel used by purge module(s).
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $loggerChannelPurge;

  /**
   * {@inheritdoc}
   */
  public function __construct(LoggerInterface $logger_channel_purge, $id, array $grants = []) {
    $this->id = $id;
    $this->grants = $grants;
    $this->loggerChannelPurge = $logger_channel_purge;
    foreach ($grants as $grant) {
      $this->access[$grant] = TRUE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getGrants() {
    return $this->grants;
  }

  /**
   * {@inheritdoc}
   */
  public function isDebuggingEnabled() {
    return $this->access[RfcLogLevel::DEBUG];
  }

  /**
   * {@inheritdoc}
   */
  public function emergency($message, array $context = []) {
    if ($this->access[RfcLogLevel::EMERGENCY]) {
      $this
        ->log(LogLevel::EMERGENCY, $message, $context);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function alert($message, array $context = []) {
    if ($this->access[RfcLogLevel::ALERT]) {
      $this
        ->log(LogLevel::ALERT, $message, $context);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function critical($message, array $context = []) {
    if ($this->access[RfcLogLevel::CRITICAL]) {
      $this
        ->log(LogLevel::CRITICAL, $message, $context);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function error($message, array $context = []) {
    if ($this->access[RfcLogLevel::ERROR]) {
      $this
        ->log(LogLevel::ERROR, $message, $context);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function warning($message, array $context = []) {
    if ($this->access[RfcLogLevel::WARNING]) {
      $this
        ->log(LogLevel::WARNING, $message, $context);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function notice($message, array $context = []) {
    if ($this->access[RfcLogLevel::NOTICE]) {
      $this
        ->log(LogLevel::NOTICE, $message, $context);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function info($message, array $context = []) {
    if ($this->access[RfcLogLevel::INFO]) {
      $this
        ->log(LogLevel::INFO, $message, $context);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function debug($message, array $context = []) {
    if ($this->access[RfcLogLevel::DEBUG]) {
      $this
        ->log(LogLevel::DEBUG, $message, $context);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function log($level, $message, array $context = []) {
    $context += [
      '@purge_channel_part' => $this->id,
    ];
    $message = '@purge_channel_part: ' . $message;
    $this->loggerChannelPurge
      ->log($level, $message, $context);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LoggerChannelPart::$access protected property Access levels for each RFC 5424 log type.
LoggerChannelPart::$grants protected property Permitted RFC 5424 log types.
LoggerChannelPart::$id protected property The identifier of the channel part.
LoggerChannelPart::$loggerChannelPurge protected property The single and central logger channel used by purge module(s).
LoggerChannelPart::alert public function Logs only when granted \Drupal\Core\Logger\RfcLogLevel::EMERGENCY. Overrides LoggerChannelPartInterface::alert
LoggerChannelPart::critical public function Logs only when granted \Drupal\Core\Logger\RfcLogLevel::CRITICAL. Overrides LoggerChannelPartInterface::critical
LoggerChannelPart::debug public function Logs only when granted \Drupal\Core\Logger\RfcLogLevel::DEBUG. Overrides LoggerChannelPartInterface::debug
LoggerChannelPart::emergency public function Logs only when granted \Drupal\Core\Logger\RfcLogLevel::EMERGENCY. Overrides LoggerChannelPartInterface::emergency
LoggerChannelPart::error public function Logs only when granted \Drupal\Core\Logger\RfcLogLevel::ERROR. Overrides LoggerChannelPartInterface::error
LoggerChannelPart::getGrants public function Retrieve given grants. Overrides LoggerChannelPartInterface::getGrants
LoggerChannelPart::info public function Logs only when granted \Drupal\Core\Logger\RfcLogLevel::INFO. Overrides LoggerChannelPartInterface::info
LoggerChannelPart::isDebuggingEnabled public function Determine whether this channel has a RfcLogLevel::DEBUG grant. Overrides LoggerChannelPartInterface::isDebuggingEnabled
LoggerChannelPart::log public function Prefixes messages with the channel ID so that the origin can be traced. Overrides LoggerChannelPartInterface::log
LoggerChannelPart::notice public function Logs only when granted \Drupal\Core\Logger\RfcLogLevel::NOTICE. Overrides LoggerChannelPartInterface::notice
LoggerChannelPart::warning public function Logs only when granted \Drupal\Core\Logger\RfcLogLevel::WARNING. Overrides LoggerChannelPartInterface::warning
LoggerChannelPart::__construct public function Construct \Drupal\purge\Logger\LoggerChannelPartInterface. Overrides LoggerChannelPartInterface::__construct