You are here

class LoggerChannel in Service Container 7

Same name and namespace in other branches
  1. 7.2 src/Logger/LoggerChannel.php \Drupal\service_container\Logger\LoggerChannel

Defines a logger channel that most implementations will use.

Hierarchy

Expanded class hierarchy of LoggerChannel

1 file declares its use of LoggerChannel
LoggerChannelTest.php in tests/src/Logger/LoggerChannelTest.php
Contains \Drupal\Tests\service_container\Logger\LoggerChannelTest.

File

src/Logger/LoggerChannel.php, line 18
Contains \Drupal\service_container\Logger\LoggerChannel.

Namespace

Drupal\service_container\Logger
View source
class LoggerChannel extends LoggerBase implements LoggerChannelInterface {

  /**
   * The name of the channel of this logger instance.
   *
   * @var string
   */
  protected $channel;

  /**
   * An array of arrays of \Psr\Log\LoggerInterface keyed by priority.
   *
   * @var array
   */
  protected $loggers = array();

  /**
   * The request stack object.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * The current user object.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * {@inheritdoc}
   */
  public function log($level, $message, array $context = array()) {
    $context += array(
      'channel' => $this->channel,
    );

    // @codeCoverageIgnore
    foreach ($this
      ->sortLoggers() as $logger) {
      $logger
        ->log($level, $message, $context);
    }
  }

  /**
   * Constructs a LoggerChannel object
   *
   * @param string $channel
   *   The channel name for this instance.
   */
  public function __construct($channel) {
    $this->channel = $channel;
  }

  /**
   * {@inheritdoc}
   */
  public function setLoggers(array $loggers) {
    $this->loggers = $loggers;
  }

  /**
   * {@inheritdoc}
   */
  public function addLogger(LoggerInterface $logger, $priority = 0) {
    $this->loggers[$priority][] = $logger;
  }

  /**
   * Sorts loggers according to priority.
   *
   * @return array
   *   An array of sorted loggers by priority.
   */
  protected function sortLoggers() {
    $sorted = array();
    krsort($this->loggers);
    foreach ($this->loggers as $loggers) {
      $sorted = array_merge($sorted, $loggers);
    }
    return $sorted;
  }

  /**
   * {@inheritdoc}
   */
  public function setRequestStack(RequestStack $requestStack = NULL) {
    $this->requestStack = $requestStack;
  }

  /**
   * {@inheritdoc}
   */
  public function setCurrentUser(AccountInterface $current_user = NULL) {
    $this->currentUser = $current_user;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LoggerBase::alert public function Action must be taken immediately. Overrides LoggerInterface::alert
LoggerBase::critical public function Critical conditions. Overrides LoggerInterface::critical
LoggerBase::debug public function Detailed debug information. Overrides LoggerInterface::debug
LoggerBase::emergency public function System is unusable. Overrides LoggerInterface::emergency
LoggerBase::error public function Runtime errors that do not require immediate action but should typically be logged and monitored. Overrides LoggerInterface::error
LoggerBase::info public function Interesting events. Overrides LoggerInterface::info
LoggerBase::notice public function Normal but significant events. Overrides LoggerInterface::notice
LoggerBase::warning public function Exceptional occurrences that are not errors. Overrides LoggerInterface::warning
LoggerChannel::$channel protected property The name of the channel of this logger instance.
LoggerChannel::$currentUser protected property The current user object.
LoggerChannel::$loggers protected property An array of arrays of \Psr\Log\LoggerInterface keyed by priority.
LoggerChannel::$requestStack protected property The request stack object.
LoggerChannel::addLogger public function Adds a logger. Overrides LoggerChannelInterface::addLogger
LoggerChannel::log public function Logs with an arbitrary level. Overrides LoggerInterface::log
LoggerChannel::setCurrentUser public function Sets the current user. Overrides LoggerChannelInterface::setCurrentUser
LoggerChannel::setLoggers public function Sets the loggers for this channel. Overrides LoggerChannelInterface::setLoggers
LoggerChannel::setRequestStack public function Sets the request stack. Overrides LoggerChannelInterface::setRequestStack
LoggerChannel::sortLoggers protected function Sorts loggers according to priority.
LoggerChannel::__construct public function Constructs a LoggerChannel object