Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Logger / LoggerChannelFactory.php
1 <?php
2
3 namespace Drupal\Core\Logger;
4
5 use Psr\Log\LoggerInterface;
6 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
7 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
8
9 /**
10  * Defines a factory for logging channels.
11  */
12 class LoggerChannelFactory implements LoggerChannelFactoryInterface, ContainerAwareInterface {
13   use ContainerAwareTrait;
14
15   /**
16    * Array of all instantiated logger channels keyed by channel name.
17    *
18    * @var \Drupal\Core\Logger\LoggerChannelInterface[]
19    */
20   protected $channels = [];
21
22   /**
23    * An array of arrays of \Psr\Log\LoggerInterface keyed by priority.
24    *
25    * @var array
26    */
27   protected $loggers = [];
28
29   /**
30    * {@inheritdoc}
31    */
32   public function get($channel) {
33     if (!isset($this->channels[$channel])) {
34       $instance = new LoggerChannel($channel);
35
36       // If we have a container set the request_stack and current_user services
37       // on the channel. It is up to the channel to determine if there is a
38       // current request.
39       if ($this->container) {
40         $instance->setRequestStack($this->container->get('request_stack'));
41         $instance->setCurrentUser($this->container->get('current_user'));
42       }
43
44       // Pass the loggers to the channel.
45       $instance->setLoggers($this->loggers);
46       $this->channels[$channel] = $instance;
47     }
48
49     return $this->channels[$channel];
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function addLogger(LoggerInterface $logger, $priority = 0) {
56     // Store it so we can pass it to potential new logger instances.
57     $this->loggers[$priority][] = $logger;
58     // Add the logger to already instantiated channels.
59     foreach ($this->channels as $channel) {
60       $channel->addLogger($logger, $priority);
61     }
62   }
63
64 }