You are here

protected function MonologLoggerChannelFactory::getChannelInstance in Monolog 8

Same name and namespace in other branches
  1. 2.x src/Logger/MonologLoggerChannelFactory.php \Drupal\monolog\Logger\MonologLoggerChannelFactory::getChannelInstance()

Factory function for Monolog loggers.

Parameters

string $channel_name: The name the logging channel.

Return value

\Psr\Log\LoggerInterface

Throws

\RuntimeException

\InvalidArgumentException

1 call to MonologLoggerChannelFactory::getChannelInstance()
MonologLoggerChannelFactory::get in src/Logger/MonologLoggerChannelFactory.php
Retrieves the registered logger for the requested channel.

File

src/Logger/MonologLoggerChannelFactory.php, line 69

Class

MonologLoggerChannelFactory
Defines a factory for logging channels.

Namespace

Drupal\monolog\Logger

Code

protected function getChannelInstance($channel_name) {
  if (!class_exists('Monolog\\Logger')) {
    throw new \RuntimeException('The Monolog\\Logger class was not found. Make sure the Monolog package is installed via Composer.');
  }
  if (!$this->container) {

    // We need the container to read parameters etc.
    return new NullLogger();
  }
  $logger = new Logger($channel_name);
  $parameters = $this->container
    ->getParameter('monolog.channel_handlers');
  $config = array_key_exists($channel_name, $parameters) ? $parameters[$channel_name] : $parameters['default'];
  $formatter = NULL;
  $handlers = $config;
  if (array_key_exists('handlers', $config)) {
    $formatter = $config['formatter'];
    $handlers = $config['handlers'];
  }
  foreach ($handlers as $handler) {

    /** @var \Monolog\Handler\HandlerInterface $h */
    $h = $this->container
      ->get('monolog.handler.' . $handler);
    if ($formatter && $this->container
      ->has('monolog.formatter.' . $formatter)) {

      /** @var \Monolog\Formatter\FormatterInterface $f */
      $f = $this->container
        ->get('monolog.formatter.' . $formatter);
      $h
        ->setFormatter($f);
    }
    $logger
      ->pushHandler($h);
  }
  foreach ($this->container
    ->getParameter('monolog.processors') as $processor) {
    $logger
      ->pushProcessor($this->container
      ->get('monolog.processor.' . $processor));
  }
  return $logger;
}