LoggerChannelFactory.php 1.7 KB

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