3 namespace Drupal\Core\Logger;
5 use Psr\Log\LoggerInterface;
6 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
7 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
10 * Defines a factory for logging channels.
12 class LoggerChannelFactory implements LoggerChannelFactoryInterface, ContainerAwareInterface {
13 use ContainerAwareTrait;
16 * Array of all instantiated logger channels keyed by channel name.
18 * @var \Drupal\Core\Logger\LoggerChannelInterface[]
20 protected $channels = [];
23 * An array of arrays of \Psr\Log\LoggerInterface keyed by priority.
27 protected $loggers = [];
32 public function get($channel) {
33 if (!isset($this->channels[$channel])) {
34 $instance = new LoggerChannel($channel);
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
39 if ($this->container) {
40 $instance->setRequestStack($this->container->get('request_stack'));
41 $instance->setCurrentUser($this->container->get('current_user'));
44 // Pass the loggers to the channel.
45 $instance->setLoggers($this->loggers);
46 $this->channels[$channel] = $instance;
49 return $this->channels[$channel];
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);