You are here

LoggerChannelFactory.php in Service Container 7.2

Same filename and directory in other branches
  1. 7 src/Logger/LoggerChannelFactory.php

File

src/Logger/LoggerChannelFactory.php
View source
<?php

/**
 * @file
 * Contains \Drupal\service_container\Logger\LoggerChannelFactory.
 */
namespace Drupal\service_container\Logger;

use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Psr\Log\LoggerInterface;

/**
 * Defines a factory for logging channels.
 */
class LoggerChannelFactory implements LoggerChannelFactoryInterface {

  /**
   * Array of all instantiated logger channels keyed by channel name.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface[]
   */
  protected $channels = array();

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

  /**
   * {@inheritdoc}
   */
  public function get($channel) {
    if (!isset($this->channels[$channel])) {
      $instance = new LoggerChannel($channel);

      // Pass the loggers to the channel.
      $instance
        ->setLoggers($this->loggers);
      $this->channels[$channel] = $instance;
    }
    return $this->channels[$channel];
  }

  /**
   * {@inheritdoc}
   */
  public function addLogger(LoggerInterface $logger, $priority = 0) {

    // Store it so we can pass it to potential new logger instances.
    $this->loggers[$priority][] = $logger;

    // Add the logger to already instantiated channels.
    foreach ($this->channels as $channel) {
      $channel
        ->addLogger($logger, $priority);
    }
  }

}

Classes

Namesort descending Description
LoggerChannelFactory Defines a factory for logging channels.